{% load recurse %}

<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="{% url 'index' %}">DjangoBlogIt</a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search From Blog">
        </div>
      </form>

      {% recurse menu.menu_set.all|dictsort:"lvl" with menu as menu %}
        <ul class="nav navbar-nav navbar-right">
          {% loop %}
            <li>
              {% if menu.has_children %}
                <a class="dropdown-toggle" data-toggle='dropdown' href="#">
                  {{ menu.title }}<span class="caret"></span>
                </a>
                <ul class="dropdown-menu multi-level" role="menu" aria-labelledby="dropdownMenu">
                  {% for child in menu.get_children %}
                    {% if child.has_children %}
                      {% include "posts/nav_submenu.html" with menu=child %}
                    {% else %}
                      <li><a href="{{child.url}}">{{ child.title }}</a></li>
                    {% endif %}
                  {% endfor %}
                </ul>
              {% else %}
                <li>
                  <a href="{{ menu.url }}">{{ menu.title }}</a>
                </li>
              {% endif %}
            </li>
          {% endloop %}
        </ul>
      {% endrecurse %}

    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
