Skip to content
This repository has been archived by the owner on Apr 11, 2018. It is now read-only.

Commit

Permalink
Fix ctx var output in imported macros. gh-363
Browse files Browse the repository at this point in the history
  • Loading branch information
paularmstrong committed Dec 3, 2013
1 parent 0d72bef commit beb37a2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/tags/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,23 @@ var utils = require('../utils');
*/
exports.compile = function (compiler, args) {
var ctx = args.pop(),
out = '_ctx.' + ctx + ' = {};\n' +
'(function (_ctx) {\n' +
' var _output = "";\n';
out = '_ctx.' + ctx + ' = {};\n var _output = "";\n',
replacements = utils.map(args, function (arg) {
return {
ex: new RegExp('_ctx.' + arg.name, 'g'),
re: '_ctx.' + ctx + '.' + arg.name
};
});

out += args.join('');
out += '}(_ctx.' + ctx + '));\n';
// Replace all occurrences of all macros in this file with
// proper namespaced definitions and calls
utils.each(args, function (arg) {
var c = arg.compiled;
utils.each(replacements, function (re) {
c = c.replace(re.ex, re.re);
});
out += c;
});

return out;
};
Expand All @@ -52,7 +63,7 @@ exports.parse = function (str, line, parser, types, stack, opts) {
}
macroName = token.args[0];
out += token.compile(compiler, token.args, token.content, [], compileOpts) + '\n';
self.out.push(out);
self.out.push({compiled: out, name: macroName});
});
return;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/import.expectation.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@
Salsa: Medium




Tacos

2 changes: 2 additions & 0 deletions tests/cases/import.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
{% endblock %}

{{ macros.nested('Medium') }}

{{ macros.global_ctx() }}
4 changes: 4 additions & 0 deletions tests/cases/macros.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
{% macro nested(salsa) %}
{{ tacos('Pork', salsa) }}
{% endmacro %}

{% macro global_ctx() %}
{{ first }}
{% endmacro %}

0 comments on commit beb37a2

Please sign in to comment.