php - twig dynamic variable call -


i passed data in 3 languages twig template , display data in way:

{% set lang=app.request.get("lang")%} {% item in contests%}     {% if lang=="fa"%}         {{item.titlefa}}     {% elseif lang=="en"%}         {{item.titleen}}     {% elseif lang=="ar"%}         {{item.titlear}}     {% endif%} {% endfor%} 

it wirking must create 3 if condition each object in "contests" how can show data in logic:

{% set lang=app.request.get("lang")%}  {{item.title~lang}} {% endfor%} 

that can call proper method in contest

you can use attribute twig function call @ runtime method name, example:

    {% set lang=app.request.get("lang")%}     {% methodname = 'title'~lang %}       {% item in contests%}         {{ attribute(item, methodname) }}       {% endfor%} 

hope help


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -