From e58abd97e685261f7dce659b861cc4bb631e923d Mon Sep 17 00:00:00 2001 From: Mihkel Eidast Date: Fri, 13 Nov 2020 20:17:04 +0200 Subject: [PATCH] fix lost context when calling a macro multiple times --- src/twig.logic.js | 1 + test/templates/macro-self-twice.twig | 2 ++ test/test.macro.js | 10 ++++++++++ 3 files changed, 13 insertions(+) create mode 100644 test/templates/macro-self-twice.twig diff --git a/src/twig.logic.js b/src/twig.logic.js index 3b3039e5..0031ac5c 100644 --- a/src/twig.logic.js +++ b/src/twig.logic.js @@ -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 diff --git a/test/templates/macro-self-twice.twig b/test/templates/macro-self-twice.twig new file mode 100644 index 00000000..375c8992 --- /dev/null +++ b/test/templates/macro-self-twice.twig @@ -0,0 +1,2 @@ +{% macro input(name, value, type, size) %}{% endmacro %} +{% import _self as forms %}

{{ forms.input('username') }}

{{ forms.input('password') }}

diff --git a/test/test.macro.js b/test/test.macro.js index e51e4a03..0e9ef6df 100644 --- a/test/test.macro.js +++ b/test/test.macro.js @@ -34,6 +34,16 @@ describe('Twig.js Macro ->', function () { twig({ref: 'import-macro-self'}).render({ }).trim().should.equal('

'); }); + 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('

'); + }); + it('it should run wrapped macro with self reference', function () { twig({ id: 'import-wrapped-macro-self',