Sunday, December 4, 2016

Is there a way to replace a string with html tags in TWIG (Symfony2)?

http://stackoverflow.com/questions/23239173/is-there-a-way-to-replace-a-string-with-html-tags-in-twig-symfony2

I have an html saved in my database with a special tag string like [special_tag] and I want to display that html to my twig template at the same time replace the [special_tag] with any html elements.
Is this possible?
shareimprove this question
Yes, you could use Twig's replace filter:
{{ my_variable_with_html | replace({'[special_tag]': 'new value'}) }}
shareimprove this answer
   
thanks for the quick response but I've already tried that. {% set var_html = "[special_tag]"%} {{ var_html|replace({"[special_tag]": "<p>new value</p>" }) }} result: <p>new value</p> expected: new value – karl2108 Apr 23 '14 at 8:58 
   
You'll have to add another filter: raw{{ var_html | replace(...) | raw }} – Crozin Apr 23 '14 at 9:18
   
Awesome! thanks! – karl2108 Apr 23 '14 at 23:03