phptwigpyrocms

How to echo within echo (Twig)


I have a problem using Twig in Pyrocms. I am trying to echo a variable within a shorthand if statement in Twig.

style="background-image: {{ (not link.bgcolor is empty ? 'linear-gradient(transparent, {{link.bgcolor}}),' : '')|raw }} url('{{link.image.url()}}');"

The statement is correct, but the displayed value actually is linear-gradient(transparent, {{link.bgcolor}}), the {{link.bgcolor}} is not getting parsed by Twig. How can I use {{}} tags within another {{}} tags?


Solution

  • You're already in Twig-context, since you've opened it with {{. So you can refer to variables without adding an additional {{ ... }}. You just need to get out of the string context and concatenate the variable with the concatenation operator ~. It should then look something like this:

    {{ (not link.bgcolor is empty ? 'linear-gradient(transparent, ' ~ link.bgcolor ~ '),' : '')|raw }}