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

strict_variables evaluating both operands of the and operator #651

Open
BrianWalters opened this issue Jun 29, 2019 · 7 comments
Open

strict_variables evaluating both operands of the and operator #651

BrianWalters opened this issue Jun 29, 2019 · 7 comments

Comments

@BrianWalters
Copy link

I am using twig.js with strict_variables set to true.

If I have a template like this:

{% if pagination|default and pagination.links|default %}
    ...
{% endif %}

and I loaded it with a completely empty context, I would expect no errors. This works in PHP Twig and what is recommended.

In twig.js, I get a TwigException:

Can't access a key links on an null or undefined object.

It looks to me that twig.js is evaluating the truthiness of both operands even if the first one is false. I would not expect this, not only for this use case, but because JavaScript doesn't work this way either.

The workaround is this:

{% if pagination|default %}
    {% if pagination.links|default %}
        ...
    {% endif %}
{% endif %}

That makes the template harder to read and creates more indenting and whitespace then normal.

@ericmorand
Copy link
Contributor

I confirm that TwigPHP "and" token only evaluates the right operand if the left one is false.

@willrowe
Copy link
Collaborator

willrowe commented Jul 1, 2019

Can you link to an example on twigfiddle?

@BrianWalters
Copy link
Author

@willrowe https://twigfiddle.com/3yjwsw

My example above in a twigfiddle. The exception I get in twig.js is:

TwigException: Can't access a key links on an null or undefined object.

@axten
Copy link

axten commented Oct 27, 2019

same problem here

@das-peter
Copy link

das-peter commented Nov 18, 2019

Same error when explicitly checking a undefined variable.
Following template should always run even if lie isn't defined and strict_variables is enabled - just the output should change:
The {{ baked_good }} is a{% if lie is defined and lie %} lie {% else %} reality {% endif %}!

However, with strict_variables enabled it fails with TwigException: Variable "lie" does not exist.
Running Example: https://jsfiddle.net/jsz9uy3h/6/

Interestingly if you use https://cdn.jsdelivr.net/npm/twig@1.13.3/twig.min.js in above fiddle it works. Looks like there's somewhere a regression.

@das-peter
Copy link

After further evaluation I suspect my comment above points to a different issue as it seem like the regression was just recently introduced - I created a dedicated issue for it: #679
Sorry for the noise here.

@willrowe
Copy link
Collaborator

I can confirm this is not working as expected, see this example: https://codepen.io/willrowe/pen/eYMeWqx

However, it does not appear to be for the reasons originally outlined. The current version of twig.js throws the error: TwigException: Variable 'pagination' does not exist.. So it appears to be hitting an issue with the default filter before even making it to the and.

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

5 participants