Mathijs Delva
Mathijs Delva

Reputation: 211

Twig: concatenate variable in string

I've just begun learning Twig and i'm really stuck at this stupid little issue. Concatenating this doesn't seem to work (eigenKleurInput will ultimately be a value):

{% set eigenKleurInput = "acefbf" %}
{% set customBackgroundColorInline = 'style=background-color: #' ~ eigenKleurInput %}

The output variable "customBackgroundColorInline" is put inside a div:

<section {{ customBackgroundColorInline }}>

Desired output would be

<section style="background-color: #xxx">

Thank you very much!

Upvotes: 0

Views: 9066

Answers (1)

Matteo
Matteo

Reputation: 39460

If I correctly understand your question the problem is about encoded character: if you add the " in your code twig render as &quot;.

In this case you should use the raw filter as follow:

{% set eigenKleurInput = "acefbf" %}
{% set customBackgroundColorInline = 'style="background-color: #' ~ eigenKleurInput ~ '"' %}


 <section {{ customBackgroundColorInline|raw }}>

So the output will be:

<section style="background-color: #acefbf">

You could try online in this working twigfiddle

Hope this help

Upvotes: 1

Related Questions