-
Notifications
You must be signed in to change notification settings - Fork 275
Open
Description
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.