8000 fix lost context when calling a macro multiple times · twigjs/twig.js@e58abd9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e58abd9

Browse files
author
Mihkel Eidast
committed
fix lost context when calling a macro multiple times
1 parent a7f0968 commit e58abd9

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

src/twig.logic.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ module.exports = function (Twig) {
10061006
state.macros[token.macroName] = function (...args) {
10071007
// Pass global context and other macros
10081008
const macroContext = {
1009+
...context,
10091010
_self: state.macros
10101011
};
10111012
// Save arguments

test/templates/macro-self-twice.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{% macro input(name, value, type, size) %}<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />{% endmacro %}
2+
{% import _self as forms %}<p>{{ forms.input('username') }}</p><p>{{ forms.input('password') }}</p>

test/test.macro.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ describe('Twig.js Macro ->', function () {
3434
twig({ref: 'import-macro-self'}).render({ }).trim().should.equal('<p><input type="text" name="username" value="" size="20" /></p>');
3535
});
3636

37+
it('it should run macro with self reference twice', function () {
38+
twig({
39+
id: 'import-macro-self-twice',
40+
path: 'test/templates/macro-self-twice.twig',
41+
async: false
42+
});
43+
// Load the template
44+
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>');
45+
});
46+
3747
it('it should run wrapped macro with self reference', function () {
3848
twig({
3949
id: 'import-wrapped-macro-self',

0 commit comments

Comments
 (0)
0