Accessing Array Values Using Array Key from Twig

Accessing array values using array key from Twig

Have you just tried this:

attribute(item2, key).title

Twig accessing array values using variables

You can use the attribute function:

{{ attribute(array, key) }}

From the doc:

addition, the defined test can check for the existence of a dynamic
attribute:

{{ attribute(object, method) is defined ? 'Method exists' : 'Method does not exist' }}

Hope this help

Get specific array element by matching key value in Twig

I don't know how your Controller (using Symfony?) looks like, but if the User is an object, you can simply use {{ user.team.name }}.

If that's not possible, you can use this:

{{ teams[user.team_id].name }}

Documentation

In case your the array keys don't match the id, you can even shorten your template:

{% for team in teams if team.id == user.team_id %}
{{team.name}}
{% endfor %}

Twig array access

Based on the var_dump of array(1) { ["title"]=> string(11) "SpaceVision" }

You should access your array in this way: {{ naziv['title'] }}.

The key of your array is associative and not a numerically indexed array. That is why you cannot use naziv[0].

You can also use: {{ naziv.title }} aswell.

See the documentation.

Twig accessing array key where is space (PHP allow space on the array key)

You can use attribute function

{{ attribute(array_name, 'Vip Client') }}

As suggested here: http://twig.sensiolabs.org/doc/templates.html#variables

Accessing Keys Arrays in Twig

If you want to access a single property, only use:

{{ Error["motherboard"]["socketError"] }}

If you want to iterate through the array, you need this:

{% for key,Error in userCustomErrors %}
{% for key2,Error2 in Error %}
<p>{{ key }}, {{ key2 }} => {{ Error2 }}</p>
{% endfor %}
{% endfor %}

That way you can iterate through all values

How to access values by key in Twig without a for loop

I figured out how to pre-process the array in Twig using the merge() function like so:

{% set parametersHash = [] %}
{% for parameter in data.resolution.section.parameters %}
{% set parametersHash = parametersHash|merge({(parameter.key): parameter.value}) %}
{% endfor %}

This allows me to access individual parameters by key like so:

{{ parametersHash["guitar.applicationId"] }}

which yields:

fHpJN0dfgdfrjjvE

Accessing array values using indexes in Twig

To access a specific array value, you can use the attribute function, it works with both array and objects:

{{ attribute(photos, 1) }}

If the array key (or object attribute) may not exist, you can add the default filter to catch exceptions and print a fallback value.

{{ attribute(photos, 1)|default('No picture') }}

How can i access to array key in twig view symfony 3

I'm not sure why you are using 2 for-loops in your code,

{% for i in  mem %} {# here you are looping members #}
{% for j in i %} {# here you are looping attributes of the member #}
{{ j }} {# output is bg, wass, http://.... #}
{% endfor %}
{% endfor %}

If you want to display the name of the members you should only use one loop

{% for i in  mem %}
<div>
<img src="{{ i.profile_pic }}" alt="{{ i.nom }} {{ i.prenom }}" title="{{ i.nom }} {{ i.prenom }}" />
{{ i.nom }} {{ i.prenom }}
</div>
{% endfor %}


Related Topics



Leave a reply



Submit