str_replace in Twig

我是研究僧i 提交于 2019-11-28 07:10:06
M Khalid Junaid

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 }) }}
websky

Use this to replace | with - and replace , width .:

{{age|replace({'|': "-", "," : "."})}}

Example input 31|60, comes out as 31-60.

umpirsky

There is a replace filter see documentation

Also, this could be useful for getting rid of (utf-8) language chars...

{{some_string|replace({'ć':'c','č':'c','š':'s','ž':'z','đ':'d','Ć':'C','Č':'C','Š':'S','Ž':'Z','Đ':'D'})}}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!