I just run in a weird behaviour in twig.
I've just used a for()
over an array that hasn't any keys set but one, which was added into the array before. For test cases, just assume this php array:
$array = array (
0 => 'test1',
1 => 'test2',
'someKey' => 'test3'
)
So, I did this in twig:
{% for key, value in array %}
{% if key == 'someKey' %}
{{ 'something special happend' }}
{% else %}
{{ 'how boring' }}
{% endif %}
{% endfor %}
What totally caught me off-guard was the fact, that something special happend
has been called twice.
After some searching I've found that this case was also true for the key 0
.
So I tried following:
{{ dump (key == 'someKey') }}
The result was
- true
- false
- true
So I did another test:
{{ dump(key|lower == 'somekey') }}
The result was
- false
- false
- true
Since I wanted to be sure, this was my last test
{{ dump(0 == 'somekey') }}
{{ dump(0|lower == 'somekey') }}
The result was
- true
- false
Why is this behaviour like it is? I can't explain it.
PS: I know by know that I can use the same as operator, but I'm just curious how this works.