8000 fix: fix lost context when calling a macro multiple times by mihkeleidast · Pull Request #760 · twigjs/twig.js · GitHub
[go: up one dir, main page]

Skip to content

fix: fix lost context when calling a macro multiple times #760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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