5

I am new to craftcms and did a look through on the documentation just recently.

I am at the part where it explain about the Categories Field and found out that we can loop through the ElementCriteriaModel with the 'nav' keyword instead of 'for'.

I checked the twig documentation but there are no 'nav' keyword :|

So which one should I use?

7

for is part of the native Twig language.

for is for general usage... It's the basic, general looping structure that you'll use 99% of the time. You'll find that it closely mirrors for and foreach in other programming languages.


nav is defined by Craft, and doesn't exist natively in Twig.

nav is special... You'll only need it to loop through entries in a Structure section, or categories in a Category Group. The sole purpose of nav is to make it easier to create a menu or navigation.

One of the biggest benefits to using the nav tag is that you'll then be able to use ifchildren within it...

{% ifchildren %}
    <ul>
        {% children %}
    </ul>
{% endifchildren %}

The logical relationship between nav and its respective children is slightly complicated... and worthy of a separate post if you need further clarification. :)

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.