1

On the menu below I need to change the class on the li from dropdown-submenu to dropdown-item for those entries that don't have children. Any ideas?

Thanks

{% set pages = craft.entries.section('services') %}
                       {% nav page in pages %}
           <li class="dropdown-submenu"> 
                {{ page.getLink() }}

                {% ifchildren %}
                   <ul class="dropdown-menu" aria-labelledby="dropdownMenuLink">
                        {% children %}
                    </ul>
                {% endifchildren %}
            </li>

        {% endnav %}
0

Have you tried something like:

<li class="{{ page.getChildren()|length == 0 ? 'dropdown-item' : 'dropdown-submenu' }}">

If that doesn't work, I suspect you could use {% ifchildren %} in concert with a variable to set the style. In this case, we set the variable to a non-null value if there are children. Then we can use the null-coalescing operator to substitute 'dropdown-item' if there are no children. Like:

{% set listclass = null %}
{% ifchildren %}
    {% set listclass = 'dropdown-submenu' %}
{% endifchildren% }
<li class="{{ listclass ?? 'dropdown-item' }}">
| improve this answer | |
  • First one did the trick. Thanks so much! – fodney Apr 26 '18 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.