Permalink
Browse files

Fixed {% include %} problems

  • Loading branch information...
1 parent 2cd4c77 commit 1230309d210871a2f6526ee8cd23e5902aad829c @leizongmin committed Mar 20, 2013
Showing with 58 additions and 21 deletions.
  1. +20 −13 lib/context.js
  2. +2 −1 lib/parser.js
  3. +0 −6 lib/vm.js
  4. +36 −1 test/tag_include.js
View
@@ -24,7 +24,6 @@ var merge = utils.merge;
* - {Integer} timeout unit:ms, default:120000
*/
var Context = module.exports = exports = function (options) {
- this._localsPrefix = '';
this._locals = {};
this._syncLocals = {};
this._asyncLocals = {};
@@ -58,6 +57,7 @@ var Context = module.exports = exports = function (options) {
set('locals');
set('syncLocals');
set('asyncLocals');
+ set('asyncLocals2');
set('filters');
set('asyncFilters');
};
@@ -85,6 +85,7 @@ Context.prototype.from = function (from) {
set('_locals');
set('_syncLocals');
set('_asyncLocals');
+ set('_asyncLocals2');
set('_filters');
set('_asyncFilters');
set('options');
@@ -368,25 +369,31 @@ Context.prototype.tablerowloopEnd = function () {
* Include a template file
*
* @param {String} name
- * @param {String} prefix
+ * @param {String} localsAst
* @param {Function} callback
*/
-Context.prototype.include = function (name, prefix, callback) {
+Context.prototype.include = function (name, localsAst, callback) {
var me = this;
- var prefixLength = 0;
if (typeof(this._includeFileHandler) === 'function') {
this._includeFileHandler(name, function (err, astList) {
if (err) return callback(err);
- if (typeof(prefix) === 'string' && prefix.length > 0) {
- me._localsPrefix += prefix + '.';
- prefixLength = prefix.length + 1;
+ if (localsAst) {
+ me.run(localsAst, function (err, locals) {
+ if (err) locals = {};
+ var c = new Context();
+ c.from(me);
+ c._asyncLocals = {};
+ c._asyncLocals2 = {};
+ c._syncLocals = {};
+ c._locals = locals;
+ c.run(astList, function (err) {
+ me.print(c.clearBuffer());
+ callback(err);
+ });
+ });
+ } else {
+ me.run(astList, callback);
}
- me.run(astList, function (err) {
- if (prefixLength > 0) {
- me._localsPrefix = me._localsPrefix.substr(0, me._localsPrefix.length - prefixLength);
- }
- return callback(err);
- });
});
} else {
return callback(new Error('please set an include file handler'));
View
@@ -315,7 +315,8 @@ var baseTags = {
'include': function (context, name, body) {
var blocks = arrayRemoveEmptyString(splitText(body, [' ']));
if (blocks.length >= 3 && blocks[1].toLowerCase() === 'with') {
- context.astStack.push(context.astNode(OPCODE.INCLUDE, stripQuoteWrap(blocks[0]), stripQuoteWrap(blocks[2])));
+ var ast = localsAstNode(stripQuoteWrap(blocks[2]), context);
+ context.astStack.push(context.astNode(OPCODE.INCLUDE, stripQuoteWrap(blocks[0]), ast));
} else {
context.astStack.push(context.astNode(OPCODE.INCLUDE, stripQuoteWrap(blocks[0])));
}
View
@@ -168,12 +168,6 @@ execOpcode[OPCODE.LOCALS] = function (context, callback, ast) {
getLocals(ast[1], ast[2], ast[3], context, callback);
};
var getLocals = function (fullName, mainName, childs, context, callback, a1, a2, a3) {
- if (context._localsPrefix) {
- // TODO: here need to optimize
- fullName = context._localsPrefix + fullName;
- childs = fullName.split('.');
- mainName = childs.shift();
- }
var info = context.getLocals(mainName);
if (info === null) {
context.throwLocalsUndefinedError(mainName);
View
@@ -16,12 +16,18 @@ describe('Tag: include', function () {
case 'file3':
var tpl = 'b={{b}}';
break;
+ case 'file4':
+ var tpl = 'v={{v}}';
+ break;
+ case 'file5':
+ var tpl = 'v={{v}}{% if c %},{% for item in c %}{% include "file5" with item %}{% unless forloop.last %},{% endunless %}{% endfor %}{% endif %}';
+ break;
default:
var tpl = '';
}
return callback(null, common.parse(tpl));
});
-
+
it('#include', function (done) {
context.setLocals('a', 123);
context.setLocals('b', 456);
@@ -56,4 +62,33 @@ describe('Tag: include', function () {
.end(done);
});
+ it('#forloop & nested', function (done) {
+ var arr = [{v:123}, {v:456}, {v:789}];
+ var arr2 = [{v: 123, c: [{v: 789}, {v: 988, c: [{v: 877}]}]}, {v: 456}];
+ context.setLocals('arr', arr);
+ context.setLocals('arr2', arr2);
+ common.taskList()
+ .add(function (done) {
+ var tpl = '{% for item in arr %}{% include "file4" with item %}{% unless forloop.last %},{% endunless %}{% endfor %}';
+ common.render(context, tpl, function (err, buf) {
+ assert.equal(err, null);
+ //console.log(buf)
+ assert.equal(buf, 'v=123,v=456,v=789');
+ context.clearBuffer();
+ done();
+ });
+ })
+ .add(function (done) {
+ var tpl = '{% for item in arr2 %}{% include "file5" with item %}{% unless forloop.last %},{% endunless %}{% endfor %}';
+ common.render(context, tpl, function (err, buf) {
+ assert.equal(err, null);
+ //console.log('done', buf)
+ assert.equal(buf, 'v=123,v=789,v=988,v=877,v=456');
+ context.clearBuffer();
+ done();
+ });
+ })
+ .end(done);
+ });
+
});

0 comments on commit 1230309

Please sign in to comment.