str_replace in Twig

umpirsky picture umpirsky · Feb 9, 2011 · Viewed 87.9k times · Source

I want to do a simple str_replace in my twig template. I'm new to twig and probably I need to add new filter or sth like that or to use existing. How can I do this? Where can I find list of filters available?

Answer

M Khalid Junaid picture M Khalid Junaid · Nov 8, 2013

To replace a string which is stored in twig variables:

{% set twig_content_variable= 'Testing to replace content'%}
{% set replace_value_var= 'Testing' %}
{% set replace_with_value_var = 'Testing complete' %} 

{{ twig_content_variable|replace({ (replace_value_var): replace_with_value_var }) }}