Permalink
Browse files

Merge branch 'bug/41'

closes gh-41
  • Loading branch information...
2 parents 06f4bd7 + 5e9afed commit 45dd93ff95dcfb29bb5abe63692884b7f21e631d @paularmstrong committed Feb 8, 2012
Showing with 27 additions and 20 deletions.
  1. +6 −6 docs/custom-tags.md
  2. +1 −1 lib/parser.js
  3. +13 −13 lib/tags.js
  4. +7 −0 tests/tags.test.js
View
12 docs/custom-tags.md
@@ -17,7 +17,7 @@ First, include the Swig parser and helpers.
Define your tag and whether or not it requires an "end" tag:
- exports.mytag = function (indent) {
+ exports.mytag = function (indent, parentBlock) {
return 'output';
};
exports.mytag.ends = true;
@@ -27,25 +27,25 @@ A Really Simple Tag <a name="example" href="#example">#</a>
To parse a swig variable with or without filters into a variable token, eg. `bar` or `foo|lowercase`
- exports.mytag = function (indent) {
+ exports.mytag = function (indent, parentBlock) {
var myArg = parser.parseVariable(this.args[0]);
return 'output';
};
exports.mytag.ends = true;
Use a parsed variable token with `helpers.setVar()` to bind a variable in your current scope into the templates scope. The `setVar` method cleans up variable output, applies filters and escaping for clean output:
- exports.mytag = function (indent) {
+ exports.mytag = function (indent, parentBlock) {
var myArg = parser.parseVariable(this.args[0]),
output = '';
output += helpers.setVar(name, myArg);
return output;
};
exports.mytag.ends = true;
-To parse the inner content of a tag for outputting, use `parser.compile.call(this, indent)`:
+To parse the inner content of a tag for outputting, use `parser.compile.apply(this, [indent, parentBlock])`:
- exports.mytag = function (indent) {
+ exports.mytag = function (indent, parentBlock) {
var myArg = parser.parseVariable(this.args[0]),
output = [];
@@ -55,7 +55,7 @@ To parse the inner content of a tag for outputting, use `parser.compile.call(thi
output.push('__output += __myArg;');
output.push('__output += "</h1>";');
output.push('__output += "<p>";');
- output.push(parser.compile.call(this, indent + ' '));
+ output.push(parser.compile.apply(this, [indent + ' ', parentBlock]));
output.push('__output += "</p>";');
return output.join('');
View
2 lib/parser.js
@@ -358,7 +358,7 @@ exports.compile = function compile(indent, parentBlock) {
} else if (token.name === 'parent') {
code += indent + ' ' + parentBlock;
} else {
- code += token.compile(indent + ' ');
+ code += token.compile(indent + ' ', parentBlock);
}
}, this);
View
26 lib/tags.js
@@ -153,7 +153,7 @@ function parseIfArgs(args) {
return tokens;
}
-exports['if'] = function (indent) {
+exports['if'] = function (indent, parentBlock) {
var thisArgs = _.clone(this.args),
args = (parseIfArgs(thisArgs)),
out = '(function () {\n';
@@ -169,15 +169,15 @@ exports['if'] = function (indent) {
out += token.value + ' ';
});
out += ') {\n';
- out += parser.compile.call(this, indent + ' ');
+ out += parser.compile.apply(this, [indent + ' ', parentBlock]);
out += '\n}\n';
out += '})();\n';
return out;
};
exports['if'].ends = true;
-exports['else'] = function (indent) {
+exports['else'] = function (indent, parentBlock) {
if (_.last(this.parent).name !== 'if') {
throw new Error('Cannot call else tag outside of "if" context.');
}
@@ -219,7 +219,7 @@ exports['else'] = function (indent) {
* <p>{{x}}</p>
* {% endfor %}
*/
-exports['for'] = function (indent) {
+exports['for'] = function (indent, parentBlock) {
var thisArgs = _.clone(this.args),
operand1 = thisArgs[0],
operator = thisArgs[1],
@@ -247,7 +247,7 @@ exports['for'] = function (indent) {
'forloop.first = (__forloopIndex === 0);\n' +
'forloop.last = (__forloopIndex === __forloopLength - 1);\n' +
'__context["' + operand1 + '"] = __forloopIter[forloop.key];\n' +
- parser.compile.call(this, indent + ' ');
+ parser.compile.apply(this, [indent + ' ', parentBlock]);
out = '(function () {\n' +
' var forloop = {}, __forloopKey, __forloopIndex = 0, __forloopLength = 0,' +
@@ -291,12 +291,12 @@ exports.empty = function (indent) {
* autoescape
* Special handling hardcoded into the parser to determine whether variable output should be escaped or not
*/
-exports.autoescape = function (indent) {
- return parser.compile.call(this, indent);
+exports.autoescape = function (indent, parentBlock) {
+ return parser.compile.apply(this, [indent, parentBlock]);
};
exports.autoescape.ends = true;
-exports.set = function (indent) {
+exports.set = function (indent, parentBlock) {
var thisArgs = _.clone(this.args),
varname = thisArgs.shift(),
value;
@@ -315,7 +315,7 @@ exports.set = function (indent) {
return 'var ' + varname + ' = ' +
'(function () {\n' +
' var __output = "";\n' +
- parser.compile.call({ tokens: [value] }, indent) + '\n' +
+ parser.compile.apply({ tokens: [value] }, [indent, parentBlock]) + '\n' +
' return __output; })();\n';
};
@@ -355,7 +355,7 @@ exports['import'] = function (indent) {
return out;
};
-exports.macro = function (indent) {
+exports.macro = function (indent, parentBlock) {
var thisArgs = _.clone(this.args),
macro = thisArgs.shift(),
args = '',
@@ -367,21 +367,21 @@ exports.macro = function (indent) {
out += '__context.' + macro + ' = function (' + args + ') {\n';
out += ' var __output = "";\n';
- out += parser.compile.call(this, indent + ' ');
+ out += parser.compile.apply(this, [indent + ' ', parentBlock]);
out += ' return __output;\n';
out += '};\n';
return out;
};
exports.macro.ends = true;
-exports.filter = function (indent) {
+exports.filter = function (indent, parentBlock) {
var thisArgs = _.clone(this.args),
name = thisArgs.shift(),
args = (thisArgs.length) ? thisArgs.join(', ') : '',
value = '(function () {\n';
value += ' var __output = "";\n';
- value += parser.compile.call(this, indent + ' ') + '\n';
+ value += parser.compile.apply(this, [indent + ' ', parentBlock]) + '\n';
value += ' return __output;\n';
value += '})()\n';
View
7 tests/tags.test.js
@@ -108,6 +108,13 @@ exports['extends'] = testCase({
test.ok((/^<pre>Error: Circular extends found on line 3 of \"extends_circular1\.html\"\!/).test(tpl.render({})), 'throws an error');
}
test.done();
+ },
+
+ 'parentBlock passed in tags': function (test) {
+ swig.compile('{% block foo %}hi{% endblock %}', { filename: 'parent' });
+ var tpl = swig.compile('{% extends "parent" %}{% block foo %}{% if true %}{% parent %}{% else %}nope{% endif %}{% endblock %}');
+ test.strictEqual(tpl(), 'hi');
+ test.done();
}
});

0 comments on commit 45dd93f

Please sign in to comment.