Skip to content

Commit

Permalink
Merge pull request #760 from mihkeleidast/issue/727
Browse files Browse the repository at this point in the history
fix: fix lost context when calling a macro multiple times
  • Loading branch information
RobLoach authored Nov 25, 2020
2 parents a7f0968 + e58abd9 commit 2c7ba19
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/twig.logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ module.exports = function (Twig) {
state.macros[token.macroName] = function (...args) {
// Pass global context and other macros
const macroContext = {
...context,
_self: state.macros
};
// Save arguments
Expand Down
2 changes: 2 additions & 0 deletions test/templates/macro-self-twice.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% macro input(name, value, type, size) %}<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />{% endmacro %}
{% import _self as forms %}<p>{{ forms.input('username') }}</p><p>{{ forms.input('password') }}</p>
10 changes: 10 additions & 0 deletions test/test.macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ describe('Twig.js Macro ->', function () {
twig({ref: 'import-macro-self'}).render({ }).trim().should.equal('<p><input type="text" name="username" value="" size="20" /></p>');
});

it('it should run macro with self reference twice', function () {
twig({
id: 'import-macro-self-twice',
path: 'test/templates/macro-self-twice.twig',
async: false
});
// Load the template
twig({ref: 'import-macro-self-twice'}).render({ }).trim().should.equal('<p><input type="text" name="username" value="" size="20" /></p><p><input type="text" name="password" value="" size="20" /></p>');
});

it('it should run wrapped macro with self reference', function () {
twig({
id: 'import-wrapped-macro-self',
Expand Down

0 comments on commit 2c7ba19

Please sign in to comment.