twig.js icon indicating copy to clipboard operation
twig.js copied to clipboard

Macro mutates data object in v1.14.0

Open kachurun opened this issue 5 years ago • 1 comments

I render templates in browser from strings like this:

const imp = '{% import _self as _macro %}';
const macro = `
	{% macro testMacro(global) %}
		<span>testMacro content</span>
	{% endmacro %}
`;
const template = `
	<div>
		{{ _macro.testMacro(_context) }}

		global.example: {{ global.example ?? 'null' }}
		example: {{ example ?? 'null' }}
                dump: {{ dump(_context) }}
	</div>
`;

const data = {
    example: 'test'
};

Twig.cache();

const tw = Twig.twig({
    id: 'test.html', 
    data: `${ macro }${ imp } ${ template }`
});

tw.renderAsync(data).then((output) => {
    console.log('output', output)
});

And its broken with version 1.14.0. All variables puts in global object ('global' is name of first argument in macro).

I get

        <div>
            <span>testMacro content</span>
            global.example: test
            example: null
            dump: 
            object(2) {
                [_self] => object(1) {
                    [testMacro] => function()
                }
              
                [global]=> object(2) {
                    [example] => string(4) "test"
                    [_macro] => object(1) {
                        [testMacro] => function()
                    }
                }
            }
        </div>

With version 1.13.3 all works fine.

kachurun avatar Dec 12 '19 13:12 kachurun