Skip to content

strict_variables evaluating both operands of the and operator #651

@BrianWalters

Description

@BrianWalters

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions