8

Is it possible to build something like this with a structure and the {% nav %} tag?

     <ul class="nav navbar-nav">
       <li class="dropdown">
         <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown</a>
         <ul class="dropdown-menu">
           <li>
               <div class="yamm-content">
                  <div class="row"> 
                    ...
           </li>
         </ul>
       </li>
     </ul>

This is my code for now:

{% set entries = craft.entries.section('pagina') %}
   <ul class="nav navbar-nav">
      {% nav entry in entries %}
         <li {{ activeClass(entry.slug) }} {% if entry.hasDescendants() %}class="dropdown yamm-fw"{% endif %}>     
            <a href="#" {% if entry.hasDescendants() %}dropdown-toggle{% endif %}" {% if entry.hasDescendants() %}data-toggle="dropdown"{% endif %}>{{ entry.title }}</a>

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

The problem is, i need custom html inside the children

9

You'll want to make use of the level property of your entry...

{% if entry.level == 1 %}
    <!-- Top Level Links -->
{% else %}
    <!-- Child Links -->
{% endif %}
|improve this answer|||||
6

For more control over nav output I like to use a recursive macro:

{% macro recursive_nav(entries, depth) %}
{% import _self as self %}
  <ul>
    {% for entry in entries %}
      <li>
        {{ entry }}
        {% if entry.hasDescendents() %}
          {{ self.recursive_nav(entry.children, depth+1) }}
        {% endif %}
      </li>
  </ul>
{% endmacro %}

{% set entries = craft.entries.section('pagina') %}
{{ recursive_nav(entries, 1) }}

You can test depth in the macro, and customize your output accordingly.

|improve this answer|||||
3

Input: creating categories in craft

<ul>
{% nav category in craft.categories.group('mainMenu').level(1) %}

{% if category.hasDescendants() %}
<li>1 Level | {{ category.title }}<ul>
  {% set subCategories = craft.categories.descendantOf(category).level(2) %}

  {% for category in subCategories %}
    {% if category.hasDescendants() %}
    <li>2 Level | {{ category.title }}<ul>
      {% set subCategories = craft.categories.descendantOf(category).level(3) %}

      {% for category in subCategories %}
      <li>3 Level | {{ category.title }}</li>
      {% if loop.last %}</ul></li>{% endif %}
      {% endfor %}

    {% else %}
    <li>2 Level | {{ category.title }}</li>

    {% endif %}
  {% if loop.last %}</ul></li>{% endif %}
  {% endfor %}

{% else %}
<li>1 Level | {{ category.title }}</li>
{% endif %}

{% endnav %}
</ul>

Output:

output

|improve this answer|||||

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.