Hey, i am wondering about how Twig works with given arrays or array-like objects internally?
My current situation is, that i have instances of the class DataBlank, which purpose is to help with RDF data.
How this class works in a nutshell: If you access data via $foo['bar'], it checks first locally, if data is available, and if not, it asks the database. That dynamic behavior allows chains like $foo['bar']['baz']['booms'].
If i want to use foo['bar']['baz']['booms'] in Twig, it complains with
Key "backmodel:page-path" in object with ArrayAccess of
class "Knorke\DataBlank" does not exist.
Here is the related code snippet:
{% for value in startpage_values %}
{# check for according usersetting #}
{% if value['backmodel:page-path'] == user['backmodel:has-user-settings']['backmodel:setting-startpage'] %}
<option value="{{ value['backmodel:page-path'] }}" selected>{{ value['rdfs:label'] }}</option>
{% else %}
<option value="{{ value['backmodel:page-path'] }}">{{ value['rdfs:label'] }}</option>
{% endif %}
{% endfor %}
Variable startpage_values is an instance of DataBlank as well as user.
The funny thing is, if i call $foo['bar']['baz']['booms'] first in PHP and use it later in Twig, the error disappears. So my guess is, that Twig uses a real array, like a copy of the original one, doesn't it?
this is because your offsetExists implementation returns true only when the data is loaded already. so when Twig asks your object whether the key exists or no, the answer depends on whether you already loaded it before or no.
The issue is in your own code. Your array-like may be happy with accessing keys that it itself reported as non-existent. We won't alter Twig to support this case (the only way is to delete any existence check in Twig, and this is not possible)
Ah thanks, great hint! That fact i didn't expect. So, to be clear, Twig first checks if a key is available, before using it or complaining about it? Thanks.
We won't alter Twig to support this case (the only way is to delete any existence check in Twig, and this is not possible)
That won't be necessary. I just wanted to get to know the stuff which is behind the scenes. Thanks for the super quick response here.
Can be closed (quickest solved issue ever? :))
Can be closed (quickest solved issue ever? :))
better click close fast than ;)
yes Twig checks first, because it is the only way it can complain about it. and this is also necessary for the . operator, which allows accessing through different ways depending of what is available: https://twig.sensiolabs.org/doc/2.x/templates.html#variables
and yes, you could close the issue (I cannot as I don't have write access to the repo)
Most helpful comment
yes Twig checks first, because it is the only way it can complain about it. and this is also necessary for the
.operator, which allows accessing through different ways depending of what is available: https://twig.sensiolabs.org/doc/2.x/templates.html#variablesand yes, you could close the issue (I cannot as I don't have write access to the repo)