How to target first or last item in a nav children loop I'm trying to use this code:

{% nav category in categories %}

   {{ category.title }}

   {% children %}

   {% if not loop.last %} BLAH {% endif %}

{% endnav %}


It appears that the {% if not loop.last %} conditional doesn't work inside of a {% nav %} tag.

How can I target/style the first and last items in each level of a nav structure?
Update Sorry, I originally thought you were building nav and just showing simplified code. If you just want to list the categories then this should work:

{% nav category in categories %}
    {{- category.title -}}
    {% if not loop.last %}, {% endif %}
    {% children %}
{% endnav %}


If your trying to build a nav system, then this should work. In either case just place your 'loop' before the children tag. See this answer for another example.

&lt;ul id="nav"&gt;
    {% nav category in categories %}

        {% if loop.first %}
            &lt;li class="first"&gt;
        {% elseif loop.last %}
            &lt;li class="last"&gt;
        {% else %}
            &lt;li&gt;
        {% endif %}

            &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;
            {% ifchildren %}
                &lt;ul&gt;
                    {% children %}
                &lt;/ul&gt;
            {% endifchildren %}
        &lt;/li&gt;
    {% endnav %}
&lt;/ul&gt;