<p><code>for</code> is <a href="http://twig.sensiolabs.org/doc/tags/for.html">part of the native Twig language</a>.</p>

<p><code>for</code> 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 <code>for</code> and <code>foreach</code> in other programming languages.</p>

<hr>

<p><code>nav</code> is <a href="https://craftcms.com/docs/templating/nav">defined by Craft</a>, and doesn't exist natively in Twig.</p>

<p><code>nav</code> is special... You'll only need it to loop through entries in a <a href="https://craftcms.com/docs/sections-and-entries#section-types">Structure section</a>, or categories in a <a href="https://craftcms.com/docs/categories">Category Group</a>. <strong>The sole purpose of <code>nav</code> is to make it easier to create a menu or navigation.</strong></p>

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

<pre><code>{% ifchildren %}
    &lt;ul&gt;
        {% children %}
    &lt;/ul&gt;
{% endifchildren %}
</code></pre>

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

CC BY-SA 3.0
2016-05-07T05:10:41.087Z
2016-05-07T05:10:41.087Z
Lindsey D
<p>I am new to craftcms and did a look through on the documentation just recently.</p>

<p>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'.</p>

<p>I checked the twig documentation but there are no 'nav' keyword :|</p>

<p>So which one should I use?</p>

<categories><navigation><loop>
What is the difference between 'nav' and 'for' for looping?
# What is the difference between 'nav' and 'for' for looping?

(Asked by: username_0 on 2016-05-07)

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?

**Tags:** categories, navigation, loop

---

## 1 answers

### Answer by username_1 on 2016-05-07. Score: 7 | Accepted answer

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 %}
    
        {% children %}
    
{% endifchildren %}

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


---

