0

I have a string in twig that needs to be replaced with some words.

{% set myvar = 'Hello World' %}

{% set firstvar = 'Hello' %}

{% set secondvar = 'Test' %}

If I want to replace Hello with the test below code doesn't work.

{{ myvar|replace({"{{firstvar}}":"{{secondvar}}"}) }}

My expected output is : Test World

What is the wrong with the code. I ve searched everywhere but I couldn't find it.

i am batman
  • 581
  • 3
  • 6
  • 21
  • From the documentation it looks like you just need to get rid of the double quotes (they're already strings) – Sterling Archer Apr 08 '19 at 18:47
  • @SterlingArcher No luck. I get below error after removing the double quotes `Uncaught PHP Exception Twig_Error_Syntax: "A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{"."` – i am batman Apr 08 '19 at 18:49
  • 3
    Ah! I think I found an answer for you. Try the marked answer [here](https://stackoverflow.com/questions/4943880/str-replace-in-twig). Looks like you have to use parentheses when replacing string vars – Sterling Archer Apr 08 '19 at 18:52
  • 1
    Possible duplicate of [How to use variables in Twig filter 'replace'](https://stackoverflow.com/questions/43794257/how-to-use-variables-in-twig-filter-replace) – DarkBee Apr 08 '19 at 19:30

0 Answers0