Tyralcori
Tyralcori

Reputation: 1047

Twig - Replace chars in connected string

How can i replace some chars (Goal is a simple double slash to one slash) in a connected twig string?

{{ config_basehost ~ navigationElement.imgSrc }} // Connect 2 Strings

Replace works like:

{{ config_basehost|replace({"a": "b"}) }} // Replace all "a" with "b"

But how can i replace something in a connected string?

{{ {{ config_basehost ~ navigationElement.imgSrc }}|replace({"a": "b"}) }} // Output: http://example.com/img/cats.jpg|replace({"a":"b"})

As you see, the replace is at the end of my "generated" URL. Same as:

{{ config_basehost ~ navigationElement.imgSrc }}|replace({"a": "b"}) // Without bracers

The double slashes only occures connecting string 1 with string 2. So, string 1 has a slash at last position inside the string, and string 2 at first position. I could replace the last char or the first char from one of these strings, yeah. But that's not the question :)

Upvotes: 1

Views: 1388

Answers (2)

Wasim Khan
Wasim Khan

Reputation: 1247

{{ STRING|replace("en": "ar") }}

Replaces all occurrences of 'e' and 'n' in a string

{{ STRING|replace({"en": "ar"}) }}

Replaces all occurrences of 'en' with ar in a string

Upvotes: 0

Warzyw
Warzyw

Reputation: 339

{{ (config_basehost ~ navigationElement.imgSrc)|replace({"a": "b"}) }} - try this.
Use brackets. Simple :)

Upvotes: 1

Related Questions