Reputation: 5597
I'm close but nothing seems to work:
{% set a %}abc{% endset %}
{% set b %}123{% endset %}
{{ "test_abc123_xyz"|replace({ '{{ a }}{{ b }}': '' }) }}
It should be obvious what I'm trying to do as I think the only part above that is wrong is this bit:
'{{ a }}{{ b }}'
...but I can't get it right (I want to use the value of a and b together and replace it).
The result of above if working would be:
test__xyz
Upvotes: 2
Views: 2051
Reputation: 7745
When you use '{{ a }}{{ b }}'
, it is nothing but a simple string to twig:
{{ '{{ a }}{{ b }}' }}
would just output:
{{ a }}{{ b }}
Then, if you want to use expression as keys in twig, you need to put them in parentheses:
{% set name = 'Adrien' %}
{% set hash = {(name): 'hello', name: 'hi'} %}
{% for key, value in hash %}
{{ key }} => {{ value }}
{% endfor %}
would output:
Adrien => hello
name => hi
So you fixed solution is:
{% set a %}abc{% endset %}
{% set b %}123{% endset %}
{{ "test_abc123_xyz"|replace({ (a ~ b): '' }) }}
The ~
is the concatenation operator in twig.
Upvotes: 6