GazleyGeester GazleyGeester - 4 months ago 22
Twig Question

How to embed double quotes in Twig interpolation?

I am trying to replace

sprintf(' href="%s%s"', ..., ...)
from a PHP file into its Twig alternative. I'd like to use Twig's interpolation feature but, the string expression has to be wrapped in double-quotes and contains embedded quotes surrounding the href attribute values.

Is there a way to do this? The docs seem fairly light weight in this regard.

{{ " href="#{value1}#{value2)"" }}

Answer

If you want to include " you need to escape the character

{{ " href=\"#{value1}#{value2)\"" }}

But it's not a good idea

A better way is to include your value directly in the attribute like :

<a href="{{url}}">Some link</a>

And of course, you can use concatenation

<a href="{{value1~value2}}">Some link</a>