Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variables as a hash keys in twig (as a parameters in path() or |trans)

Tags:

twig

symfony

Is it possible to use trans filter or path function dynamically?With variables as parameters? ex1:

{{ path('object_edit', { parameter_type : parameter_value }) }}

because: I don't know if "object" using id or slug for routing

ex2:

{{message|trans({ parameter_type : parameter_value }, 'TranslationDomain') }}

because: i don't know if it will be '%user%' or '%article%' or something else

Why this works fine:

{{ path('object_edit', { 'id' : parameter_value }) }}

but this doesn't:

{{ set parameter_type = 'id' }}
{{ path('object_edit', { parameter_type : parameter_value }) }}
like image 895
jacobmaster Avatar asked Jan 17 '12 10:01

jacobmaster


2 Answers

{% set key = 'foobar' %}
{% set hash = { (key) : 'hello world' } %}
{% debug hash %}

prints:

array(1) { ["foobar"]=> string(11) "hello world" } 

wrapping in parenthesis solves your problem.

like image 182
s10z Avatar answered Oct 15 '22 02:10

s10z


Enclose the hash keys with parenthesis (not braces).

{{ path('object_edit', { (parameter_type) : parameter_value }) }}
like image 19
iambray Avatar answered Oct 15 '22 04:10

iambray