Skip to content

Commit

Permalink
Fix filter block tag (fixes #576).
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Mar 14, 2016
1 parent e1c3753 commit 647f9d5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
17 changes: 12 additions & 5 deletions src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,6 @@ var Compiler = Object.extend({
compileFilter: function(node, frame) {
var name = node.name;
this.assertType(name, nodes.Symbol);

this.emit('env.getFilter("' + name.value + '").call(context, ');
this._compileAggregate(node.args, frame);
this.emit(')');
Expand Down Expand Up @@ -529,11 +528,9 @@ var Compiler = Object.extend({
this.emitLine(';');
}
else {
this.emitLine(ids.join(' = ') + ' = (function() {');
this.emitLine('var output = "";');
this.emit(ids.join(' = ') + ' = ');
this.compile(node.body, frame);
this.emitLine('return output;');
this.emitLine('})();');
this.emitLine(';');
}

lib.each(node.targets, function(target, i) {
Expand Down Expand Up @@ -1046,6 +1043,16 @@ var Compiler = Object.extend({
this.compileLiteral(node, frame);
},

compileCapture: function(node, frame) {
this.emitLine('(function() {');
this.emitLine('var output = "";');
this.withScopedSyntax(function () {
this.compile(node.body, frame);
});
this.emitLine('return output;');
this.emitLine('})()');
},

compileOutput: function(node, frame) {
var children = node.children;
for(var i=0, l=children.length; i<l; i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ var Extends = TemplateRef.extend('Extends');
var Include = Node.extend('Include', { fields: ['template', 'ignoreMissing'] });
var Set = Node.extend('Set', { fields: ['targets', 'value'] });
var Output = NodeList.extend('Output');
var Capture = Node.extend('Capture', { fields: ['body'] });
var TemplateData = Literal.extend('TemplateData');
var UnaryOp = Node.extend('UnaryOp', { fields: ['target'] });
var BinOp = Node.extend('BinOp', { fields: ['left', 'right'] });
Expand Down Expand Up @@ -255,6 +256,7 @@ module.exports = {
Pair: Pair,
Dict: Dict,
Output: Output,
Capture: Capture,
TemplateData: TemplateData,
If: If,
IfAsync: IfAsync,
Expand Down
16 changes: 11 additions & 5 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,11 @@ var Parser = Object.extend({
tag.colno);
}
else {
node.body = this.parseUntilBlocks('endset');
node.body = new nodes.Capture(
tag.lineno,
tag.colno,
this.parseUntilBlocks('endset')
);
node.value = null;
this.advanceAfterBlockEnd();
}
Expand Down Expand Up @@ -1025,7 +1029,11 @@ var Parser = Object.extend({
var args = this.parseFilterArgs(name);

this.advanceAfterBlockEnd(filterTok.value);
var body = this.parseUntilBlocks('endfilter');
var body = new nodes.Capture(
name.lineno,
name.colno,
this.parseUntilBlocks('endfilter')
);
this.advanceAfterBlockEnd();

var node = new nodes.Filter(
Expand All @@ -1035,9 +1043,7 @@ var Parser = Object.extend({
new nodes.NodeList(
name.lineno,
name.colno,
// Body is a NodeList with an Output node as a child,
// need to strip those
body.children[0].children.concat(args)
[body].concat(args)
)
);

Expand Down
2 changes: 1 addition & 1 deletion tests/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1476,7 +1476,7 @@
it('should work with blocks in the body', function(done) {
equal('{% extends "filter-block.html" %}' +
'{% block block1 %}force{% endblock %}',
'may the forth be with you');
'may the forth be with you\n');
finish(done);
});
});
Expand Down

0 comments on commit 647f9d5

Please sign in to comment.