From beb37a2633adb29490c9d1792aac07dd86cb13a3 Mon Sep 17 00:00:00 2001 From: Paul Armstrong Date: Mon, 2 Dec 2013 22:03:44 -0800 Subject: [PATCH] Fix ctx var output in imported macros. gh-363 --- lib/tags/import.js | 23 +++++++++++++++++------ tests/cases/import.expectation.html | 4 ++++ tests/cases/import.test.html | 2 ++ tests/cases/macros.html | 4 ++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/tags/import.js b/lib/tags/import.js index 7f4a73d9..45ecd9b5 100644 --- a/lib/tags/import.js +++ b/lib/tags/import.js @@ -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; }; @@ -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; } diff --git a/tests/cases/import.expectation.html b/tests/cases/import.expectation.html index 8dbe5dfa..6d2ddaea 100644 --- a/tests/cases/import.expectation.html +++ b/tests/cases/import.expectation.html @@ -18,3 +18,7 @@ Salsa: Medium + + +Tacos + diff --git a/tests/cases/import.test.html b/tests/cases/import.test.html index 986fa2b7..ca6b944a 100644 --- a/tests/cases/import.test.html +++ b/tests/cases/import.test.html @@ -6,3 +6,5 @@ {% endblock %} {{ macros.nested('Medium') }} + +{{ macros.global_ctx() }} diff --git a/tests/cases/macros.html b/tests/cases/macros.html index b13b7cbe..1bd0149f 100644 --- a/tests/cases/macros.html +++ b/tests/cases/macros.html @@ -17,3 +17,7 @@ {% macro nested(salsa) %} {{ tacos('Pork', salsa) }} {% endmacro %} + +{% macro global_ctx() %} +{{ first }} +{% endmacro %}