<p>You can use <code>entry.level</code> in a conditional:</p>

<pre><code>{% set entries = craft.entries.section('spb') %}
{% nav entry in entries %}

    {% set liTagAttributes = entry.level == 1 ? 'class="uk-parent" data-uk-dropdown' : 'class="uk-child"' %}

    &lt;li {{ liTagAttributes|raw }}&gt;
        &lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;
        {% ifchildren %}
            ...
        {% endifchildren %}
    &lt;/li&gt;

{% endnav %}
</code></pre>

<p>I set a intermediate var to keep the template code clean. If you don't have many levels in your structure, you could also consider to not use the <code>nav</code> tag at all and nest your navigation levels using <code>hasDescendents</code> and <code>getDescendents(1)</code> (see the example code I <a href="https://craftcms.stackexchange.com/a/1847/125">posted here</a>).</p>

CC BY-SA 3.0
2014-12-18T09:27:14.100Z
Very useful tip! Thank you. By the way, you don't need to use double quotes in `{% set liTagAttributes = entry.level == 1 ? 'class=uk-parent data-uk-dropdown' : 'class=uk-child' %}`
Stanislav Ulver
CC BY-SA 3.0
2014-12-18T09:31:54.460Z
Errrm, this wasn't for the Twig code but to make it return valid HTML for setting the CSS class, @Stanislav.
carlcs
CC BY-SA 3.0
2014-12-18T09:37:11.270Z
Sure, but if you leave them it returns two times double quotes `""...""`
Stanislav Ulver
CC BY-SA 3.0
2014-12-18T09:37:13.340Z
Sorry, you were right. HTML5 does indeed allow you to NOT wrap attribute values in quotes.
carlcs
CC BY-SA 3.0
2014-12-18T09:43:49.200Z
@Stanislav forgot to add the `raw` filter to the output. This makes Twig to not escape the code and you end up with single quotes. I definitely don't recommend to use attribute value without quotes at all.
carlcs
CC BY-SA 3.0
2014-12-17T18:32:26.867Z
2014-12-18T09:41:36.710Z
2017-04-13T12:51:00.253Z
carlcs
<p>Doing my first site with Craft and UiKit framework I encountered a small problem with dropdown menu. Here is my code:</p>

<pre><code>&lt;ul&gt;
  {% set entries = craft.entries.section('spb') %}
  {% nav entry in entries %}
  &lt;li class="uk-parent" data-uk-dropdown&gt;
    &lt;a {% if craft.request.getPath() == entry.uri %} class="active" {% endif %} href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;
    {% ifchildren %}
    &lt;div class="uk-dropdown"&gt;
      &lt;ul class="uk-nav uk-nav-dropdown"&gt;
        {% children %}
      &lt;/ul&gt;
    &lt;/div&gt;
    {% endifchildren %}
  &lt;/li&gt;
  {% endnav %}
&lt;/ul&gt;
</code></pre>

<p>The point is that my child element looks exactly like parent because it takes its structure. But I don't need neither "class" nor "data-uk-dropdown" element...</p>

<pre><code>&lt;li class="uk-parent" data-uk-dropdown=""&gt;&lt;a href="#"&gt;...&lt;/a&gt;&lt;/li&gt;
</code></pre>

<p>Is there any workaround? Thanks.</p>

<navigation><drop-down>
Navigation drop-down problem
# Navigation drop-down problem

(Asked by: username_0 on 2014-12-17)

Doing my first site with Craft and UiKit framework I encountered a small problem with dropdown menu. Here is my code:

  {% set entries = craft.entries.section('spb') %}
  {% nav entry in entries %}
  
    {{ entry.title }}
    {% ifchildren %}
    
      
        {% children %}
      
    
    {% endifchildren %}
  
  {% endnav %}


The point is that my child element looks exactly like parent because it takes its structure. But I don't need neither "class" nor "data-uk-dropdown" element...
...

Is there any workaround? Thanks.

**Tags:** navigation, drop-down


---

## 1 answers

### Answer by username_1 on 2014-12-17. Score: 2 | Accepted answer

You can use entry.level in a conditional:
{% set entries = craft.entries.section('spb') %}
{% nav entry in entries %}

    {% set liTagAttributes = entry.level == 1 ? 'class="uk-parent" data-uk-dropdown' : 'class="uk-child"' %}

    
        {{ entry.title }}
        {% ifchildren %}
            ...
        {% endifchildren %}
    

{% endnav %}

I set a intermediate var to keep the template code clean. If you don't have many levels in your structure, you could also consider to not use the nav tag at all and nest your navigation levels using hasDescendents and getDescendents(1) (see the example code I posted here).

> **Comments:**
> **username_0**: Very useful tip! Thank you. By the way, you don't need to use double quotes in `{% set liTagAttributes = entry.level == 1 ? 'class=uk-parent data-uk-dropdown' : 'class=uk-child' %}`  
> **username_1**: Errrm, this wasn't for the Twig code but to make it return valid HTML for setting the CSS class, @Stanislav.  
> **username_0**: Sure, but if you leave them it returns two times double quotes `""...""`  
> **username_1**: Sorry, you were right. HTML5 does indeed allow you to NOT wrap attribute values in quotes.  
> **username_1**: @Stanislav forgot to add the `raw` filter to the output. This makes Twig to not escape the code and you end up with single quotes. I definitely don't recommend to use attribute value without quotes at all.  

---

