Charis
Charis

Reputation: 137

In Octobercms item.viewBag.cssClass value with more than one classes use if statement with every class separately

In Octobercms Pages Plugin i can insert a CSS Class in menu item attributes. Then i can renter this value in the template using "item.viewBag.cssClass". So i can use:

{% if subitem.viewBag.cssClass == 'name' %}<li class="myname"></li>{% endif %}

The problem is when i want to use more than one classes in CSS class field.For example if i insert 2 classes in the field, name1 name2.Then i can use this:

{% if subitem.viewBag.cssClass == 'name1 name2' %}<li class="myname"></li>{% endif %}

But not this:

{% if subitem.viewBag.cssClass == 'name1' %}<li class="myname1"></li>{% endif %}

or this:

{% if subitem.viewBag.cssClass == 'name2' %}<li class="myname2"></li>{% endif %}

Is there a way on twig to identify a separate class inside of a value with 2 or more classes, so i can use if statement using any class i want?

Upvotes: 0

Views: 335

Answers (1)

Joseph Oppegaard
Joseph Oppegaard

Reputation: 621

Yes, in Twig you can use the Containment Operator.

As an example, your last conditional would change to:

{% if 'name2' in subitem.viewBag.cssClass %}<li class="myname2"></li>{% endif %}

Upvotes: 1

Related Questions