arrays - TWIG - variable == objectattribute -


i new twig , symfony2 (and php well). have array of objects can access attribute doing {{result.attribute1}} after {for result in results} statement.

i use variable {{var| removepath}} (where removepath extension made in twig), , use iterate on results array. return row if {{var| removepath}} == result.attribute1.

is possible in twig? if so, how should approach this? have tried similar code found below, "else" statement executed (saying there's no match). tried var == result.attribute, didn't work either.

{% result in results %}  {% if var|removepath in result.attribute1 %}    {{ var | removepath }} exists. {% else %}    {{ var | removepath }} doesn't exist in array. {% endif %}{% endfor %} 

thus think have 2 questions;

1) possible in twig (if so, how)

2) there easier/better way whole row (object array contains attributes 2, 3 & 4, , return in common attribute 1 (if matches var))

see schema of array recently added

thank in advance help! hope question understandable :s

just surround expression brackets.

use (var|removepath) groups expression.

example:

{% if ( 1+1 ) == 2 %} 

and can use attribute() function access properties , methods of object/array.

{{ attribute(object, method) }} {{ attribute(object, method, arguments) }} {{ attribute(array, item) }} 

attribute chapter in twig docs.

further shorten template code using ternary operator this:

{% set strippedvar = var|removepath %} {{ strippedvar }}{{ (strippedvar in result.attribute1) ? 'exists' : 'does not exist' }}. 

setting filtered variable prevents executing filter multiple times.


Comments