Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
2betop committed Nov 21, 2014
1 parent e86f97a commit 9716fd9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@ var tags = [
"featureelse",
"spage"
];
var swigInstance;

/**
* Opitions 说明
* - `view` 模板文件
* - `locals` 模板变量
* - `views` 模板根目录
* - `loader` 模板加载器,默认自带,可选。
*
* layer 参数,为 yog-view 的中间层,用来扩展模板能力。
* 比如通过 addScript, addStyle 添加的 js/css 会自动在页面开头结尾处才输出。
*
* 更多细节请查看 yog-view
*
* @return {Readable Stream}
*
* @return {Readable Stream}
*/
var SwigWrap = module.exports = function SwigWrap(options, layer) {

Expand All @@ -43,26 +42,33 @@ var SwigWrap = module.exports = function SwigWrap(options, layer) {
// 重写 loader, 让模板引擎,可以识别静态资源标示。如:example:static/lib/jquery.js
options.loader = options.loader || loader(layer, options.views);

var swig = this.swig = new Swig(options);
var swig = this.swig = swigInstance = options.cache && swigInstance || new Swig(options);
this.options = swig.options;

tags.forEach(function (tag) {
var t = require('./tags/' + tag);
swig.setTag(tag, t.parse, t.compile, t.ends, t.blockLevel || false);
});

Readable.call(this, null);

this.buzy = false;
};

util.inherits(SwigWrap, Readable);

SwigWrap.prototype._read = function(n) {
if (!this.buzy && this.options.view) {
this.renderFile(this.options.view, this.options.locals);
if (!this.buzy && this.view) {
this.renderFile(this.view, this.locals);
}
};

SwigWrap.prototype.makeStream = function(view, locals) {
Readable.call(this, null);
this.view = view;
this.locals = locals;
return this;
};

// 不推荐直接调用
// 最后在初始化 SwigWrap 的时候指定 view 已经 locals.
// 此方法将会自动调用。
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yog-swig",
"version": "0.0.16",
"version": "0.0.17",
"description": "custom swig template, add some tag or filter ",
"main": "index.js",
"directories": {
Expand Down

0 comments on commit 9716fd9

Please sign in to comment.