Reputation: 7072
I have a variable in jinja2: test1 = "this is value1 and this is value2"
with:
{{ test1 | replace("value1","my_value1") }}
I can replace value1 but I also need to replace value2 how can I do this?
I tried:
{{ test1 | replace("value1","my_value1") | replace("value2","my_value2") }}
But this then only replaces value2.
Upvotes: 32
Views: 98141
Reputation: 4920
currently you can replace variable by jinja
http://jinja.pocoo.org/docs/2.10/templates/#replace
{{ "Hello World"|replace("Hello", "Goodbye") }}
-> Goodbye World
{{ "aaaaargh"|replace("a", "d'oh, ", 2) }}
-> d'oh, d'oh, aaargh
Upvotes: 5
Reputation: 312038
Your expression seems to work just fine. If I create a template with the jinja2 expression from your question:
>>> import jinja2
>>> t = jinja2.Template('{{ test1 | replace("value1","my_value1") | replace("value2","my_value2") }}')
And then render it, passing in a value for test1
that contains both
of the target replacement strings:
>>> output = t.render(test1="this has both value1 and value2")
I get a string that has both values replaced:
>>> print (output)
this has both my_value1 and my_value2
>>>
Upvotes: 39