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?
            Asked
            
        
        
            Active
            
        
            Viewed 1.3e+01k times
        
    60
            
            
        - 
                    What exactly do you want to do? There are a number of filters and some HTML escaping built in to twig already: http://www.twig-project.org/doc/templates.html – CamelBlues Feb 22 '11 at 14:34
5 Answers
75
            
            
        Use this to replace | with - and replace , width .:
{{age|replace({'|': "-", "," : "."})}}
Example input 31|60, comes out as 31-60.
 
    
    
        Thomas Landauer
        
- 7,857
- 10
- 47
- 99
 
    
    
        websky
        
- 3,047
- 1
- 33
- 31
- 
                    4
- 
                    2@Prinsig indeed. From the documentation I thought you could only replace placeholders. – ohvitorino Aug 24 '17 at 07:33
65
            
            
        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 }) }}
 
    
    
        Thomas Landauer
        
- 7,857
- 10
- 47
- 99
 
    
    
        M Khalid Junaid
        
- 63,861
- 10
- 90
- 118
- 
                    12Thank you! The parentheses around `replace_value_var` are missing from the documentation. – Nate Cook Sep 01 '14 at 16:07
- 
                    1
- 
                    
- 
                    1The documentation simply doesn't mention the parentheses because you don't need them. – Capsule Dec 14 '17 at 03:26
- 
                    @Capsule you'd think so, but for some strange reason, it doesn't replace without them, they don't have to be for explicit string key like `'some'`, but has to be around variable (probably some internal string=>key resolution) – jave.web Feb 27 '23 at 14:33
10
            
            
        When we add a single quote inside a single quote then we face the error.
solution for that:-
{{description|replace({"\'": "'"})|raw }}
 
    
    
        Ash
        
- 165
- 2
- 3
9
            
            
        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'})}}
 
    
    
        Denis Solakovic
        
- 245
- 3
- 12
- 
                    4
- 
                    3@umpirsky Hmmm, i think the code for twig goes something like this {{ data|convert_encoding('UTF-8', 'iso that you want') }}, but i'm not 100% sure – Denis Solakovic Oct 19 '15 at 08:57
- 
                    1
0
            
            
        Use this to replace . with "" , replace - width "" and : replace _ width "" :
- "" = nothing or NULL - {{signature3.user.username|replace({'.': "", "-" : "","_":""})}}
input najim_el-guennouni, out as najimelguennouni.
 
    
    
        najim el guennouni
        
- 357
- 2
- 7
