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

Using operator as variable name fails in some case

Open ericmorand opened this issue 6 years ago • 2 comments

Twig allows using operators as variable names - this is expected since it is possible to add operators to the language and it would make the language extremely unreliable to forbid calling a variable like an operator.

But twig.js doesn't seem to support that feature for all operators.

The following template fails for example:

  • https://twigfiddle.com/coze8i
{% for in in in %}
    {{ in }}
{% endfor %}

TypeError: Cannot read property 'length' of undefined

  • https://twigfiddle.com/coze8i/2
{{ in }}

TypeError: Cannot read property 'length' of undefined

  • https://twigfiddle.com/coze8i/3
{{ matches }}

TypeError: Cannot read property 'test' of undefined

The following one succeed:

  • https://twigfiddle.com/coze8i/5
{{ is }}

Here is the script I use to test the templates above:

const Twig = require('twig');

Twig.renderFile('./index.twig', {in: [1, 2], matches: 'foo', is: 'bar'}, (err, html) => {
    console.warn(html);
});

ericmorand avatar Jun 25 '19 14:06 ericmorand

Examples in twig.js: https://codepen.io/willrowe/pen/JjLONqP

willrowe avatar Jul 28 '22 20:07 willrowe

All examples in one Twigfiddle: https://twigfiddle.com/tbav9k

willrowe avatar Jul 28 '22 20:07 willrowe