Skip to content
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

RenderFile mutate placeholders #710

Open
mimol91 opened this issue Jan 28, 2020 · 1 comment
Open

RenderFile mutate placeholders #710

mimol91 opened this issue Jan 28, 2020 · 1 comment

Comments

@mimol91
Copy link

mimol91 commented Jan 28, 2020

renderfile currently mutate placeholders, which can leads to unexpected behavior. Instead of that renderFile (and {% set someVar = true %} should create copy).

Example

{# views/a.twig #}
{% set someVar = 'ABC' %}
const Twig = require('twig');
const util = require('util');
const renderFile = util.promisify(Twig.renderFile);

const run = async () => {
	const placeholders = {};
	
	const content = await renderFile('./views/a.twig', placeholders);
	console.log(placeholders); //Placeholders will contain someVar
}
run()
  .then(a => console.log(a))
  .catch(e => console.error(e));

Now imagine that in views directory there is a list of templates to render. For example:

{# views/b.twig #}
{% if someVar is defind %}thisShouldNeverBeTrue{% endif %} 

And run function iterate over all files in views directory.
The expected result of b.twig is empty, however because someVar was created while rendering a.twig and it mutate original placeholders, it will display thisShouldNeverBeTrue.

Of course its easy to create copy of placeholders and use if for each of render.
However it's not obvious that you have to do it, and may safe some debuging hours to find a reason why its behaving like that.

@willrowe
Copy link
Collaborator

willrowe commented Aug 1, 2022

I can confirm this is a bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants