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

Array comparisons (in and ==) do not conform to TwigPHP #691

Open
dorian-marchal opened this issue Dec 11, 2019 · 3 comments · May be fixed by #883
Open

Array comparisons (in and ==) do not conform to TwigPHP #691

dorian-marchal opened this issue Dec 11, 2019 · 3 comments · May be fixed by #883

Comments

@dorian-marchal
Copy link
Contributor

dorian-marchal commented Dec 11, 2019

in operator doesn't work like Twig PHP when comparing arrays

For example, this evaluates to "true" with Twig PHP but "false" with twig.js:

{{ ["foo", "bar"] in [['foo', 'bar'], ['qux', 'baz']] ? 'true' : 'false' }}

This falls in this test:

if (b.indexOf !== undefined) {
// String
return (a === b || a !== '') && b.indexOf(a) > -1;
}

indexOf is not undefined for arrays.

Also, == operator is broken with array (it only seems to compare array lengths):

{{ ['foo'] == ['bar'] ? 'true' : 'false' }}

Evaluates to "true" with twig.js but "false" with Twig PHP.

This time, I think the corresponding code is here:

if (operator !== 'in' && operator !== 'not in' && operator !== '??') {
if (a && Array.isArray(a)) {
a = a.length;
}
if (b && Array.isArray(b)) {
b = b.length;
}
}

@dorian-marchal dorian-marchal changed the title in operator doesn't work like Twig PHP when comparing arrays Array comparison is broken Dec 11, 2019
@dorian-marchal dorian-marchal changed the title Array comparison is broken Array comparison is broken (in and ==) Dec 11, 2019
@ericmorand
Copy link
Contributor

Twig.js doesn't support Twig comparison rules. They are quite complex (and arguably debatable) because Twig inherits them from PHP.

@dorian-marchal
Copy link
Contributor Author

dorian-marchal commented Dec 14, 2019

OK, I understand.

To me this is not only a compatibility issue between Twig JS and Twig PHP.
This kind of array comparison helps to write more concise templates:

{# For example: #}
{% if [country, city, place] in [
  ["France", "Paris", "Palais de l'Élysée"],
  ["Germany", "Berlin", "Federal Chancellery"],
  ...
] %}
    Official residence
{% endif %}

{# instead of: #}
{% if
    (country == "France" and city == "Paris" and place == "Palais de l'Élysée")
    or (country == "Germany" and city == "Berlin" and place == "Federal Chancellery")
    ...
%}
    Official residence
{% endif %}

I suppose I could give an other try to Twing but it is really heavy and I need it on client side.

@dorian-marchal dorian-marchal changed the title Array comparison is broken (in and ==) Array comparisons (in and ==) are not consistent with TwigPHP Feb 27, 2020
@dorian-marchal dorian-marchal changed the title Array comparisons (in and ==) are not consistent with TwigPHP Array comparisons (in and ==) do not conform to TwigPHP Feb 27, 2020
@willrowe
Copy link
Collaborator

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

Successfully merging a pull request may close this issue.

3 participants