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

Commit

Permalink
Merge pull request #502 from logie17/master
Browse files Browse the repository at this point in the history
Allow macro output to be assigned via `set` tag. Fixes gh-499
  • Loading branch information
paularmstrong committed Jul 29, 2014
2 parents 02560d5 + 7ce80b4 commit 0d465ad
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/tags/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ exports.compile = function (compiler, args, content, parents, options, blockName
return '_ctx.' + fnName + ' = function (' + args.join('') + ') {\n' +
' var _output = "";\n' +
' __ctx = _utils.extend({}, _ctx),\n' +
' _ctx = _utils.extend({}, __ctx);\n' +
' _utils.each(_ctx, function (v, k) {\n' +
' if (["' + args.join('","') + '"].indexOf(k) !== -1) { delete _ctx[k]; }\n' +
' });\n' +
compiler(content, parents, options, blockName) + '\n' +
' _ctx = __ctx;\n' +
' _ctx = _utils.extend(_ctx, __ctx);\n' +
' return _output;\n' +
'};\n' +
'_ctx.' + fnName + '.safe = true;\n';
Expand Down
4 changes: 4 additions & 0 deletions tests/tags/macro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ describe('Tag: macro', function () {
it('gh-457: local context is copied and overwritten within macro context', function () {
expect(swig.render('{% set foo = 1 %}{% set baz = 3 %}{% macro bar(foo) %}{{ foo }}{{ baz }}{% endmacro %}{{ bar(2) }}{{ foo }}')).to.equal('231');
});

it('gh-499: a macro can be set to a variable', function () {
expect(swig.render('{% macro burrito() %}burrito{% endmacro %}{% set foo = burrito() %}{{foo}}')).to.equal('burrito');
});
});

0 comments on commit 0d465ad

Please sign in to comment.