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
Post a Comment