From e58abd97e685261f7dce659b861cc4bb631e923d Mon Sep 17 00:00:00 2001 From: Mihkel Eidast Date: Fri, 13 Nov 2020 20:17:04 +0200 Subject: [PATCH 1/2] 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', From 2916244c95bb26a48f44d879e69cf06dbb425fa7 Mon Sep 17 00:00:00 2001 From: Rob Loach Date: Fri, 27 Nov 2020 18:15:41 -0500 Subject: [PATCH 2/2] Update to 1.15.4 --- CHANGELOG.md | 5 +++++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddb33ea8..7c4edf08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +Version 1.15.4, released 2020-11-27 +----------------------------------- +Minor improvements: +* Fix lost context when calling a macro multiple times ([#727](https://github.com/twigjs/twig.js/pull/727)) by [mihkeleidast ](https://github.com/mihkeleidast) + Version 1.15.3, released 2020-11-05 ----------------------------------- Minor improvements: diff --git a/package-lock.json b/package-lock.json index 87e85038..e5fb24ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "twig", - "version": "1.15.3", + "version": "1.15.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7c510a57..52433ef5 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "John Roepke (http://john.sh/)", "name": "twig", "description": "JS port of the Twig templating language.", - "version": "1.15.3", + "version": "1.15.4", "homepage": "https://github.com/twigjs/twig.js", "license": "BSD-2-Clause", "licenses": [