0

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?

Charis
  • 117
  • 2
  • 4
  • 12

1 Answers1

1

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 %}
Joseph Oppegaard
  • 611
  • 5
  • 12
  • I missed that feature..This is exactly what i need. Thank you. – Charis Nov 01 '18 at 11:39
  • One relevant question.. Can i use Containment Operator with this logic:{% if 'first.class' in subitem.viewBag.cssClass %}
  • {% endif %} so i can display any class i use first, inside field? – Charis Nov 01 '18 at 11:53
  • I don't fully understand your question, but to break it down a bit... your if statement would check if the hardcoded string 'first.class' is in the subitem.viewBag.cssClass variable. Then, in your
  • , the {{ first.class }} code would assume that you have a variable in your twig scope with class as a attribute on that variable. I guess I'm not sure what you're going after here.
  • – Joseph Oppegaard Nov 01 '18 at 22:36
  • For example subitem.viewBag.cssClass = 'dropdown fadeIn'. If i want to use fadeIn class i have to use {% if 'fadeIn' in subitem.viewBag.cssClass %}
  • {% endif %}. If class is fadeOut i have to use one other if statement e.t.c. But if i could use second.class i will be able to display the second class logic like this:{% if there is a second class in subitem.viewBag.cssClass %}{{ display second.class }}{% endif %}. I guess that is not possible.. – Charis Nov 02 '18 at 12:43