1

I want to have an html element with two classes defined by twig variables. I can do it with one twig variable and they both work separately. But once I try to have them together, only the first class is effective. I searched on the forum but found only about twig classes or two classes direct in html. With Twig I have:

<p class={{"type#{item.type1}"}}> Paragraph </p>

In html it should be:

<p class="type1 type2"}}

When I try to combine both as below, it does not work:

<p class={{"type#{item.type1} type#{item.type2}"}} > Paragraph </p>

I also have tried the other concat method with ~ but without sucess. How to concatenate strings in twig

And instead of the space I have tried to add &nbsp; as explained here also unsucessful: How to add space between variables in twig template?

Shaker81
  • 47
  • 1
  • 12

1 Answers1

3

Your forgot the wrap your attribute value in quotes. HTML will treat the 2nd class as another attribute, not being part of the class attribute

<p class="{{"type#{item.type1} type#{item.type2}"}}">Paragraph</p>
DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • Thank you ! It worked. I thought the quotes contained within {{ }} were wraping the attribute. – Shaker81 Sep 09 '18 at 15:17
  • 1
    No because twig will treat those as the start and the end of string e.g `{{ "foo" }}` will output `foo` – DarkBee Sep 09 '18 at 15:29