Need Help with for loop in for loop

Hey guys, I’m trying to get the following output, where the ul id=“nav_desktop_werke” is only created underneath the li class=“menu-item-werke” when the url contains “werke” (sort of a dropwdown menu)

  <nav id="nav_desktop" class="hidden">
    <h1 class="logo"><a href="/">Fabian Widukind <br>Penzkofer</a></h1>
    <ul>
      <li class="menu-item-jetzt">
        <a href="/">Jetzt</a>
      <li class="menu-item-werke">
        <a href="/werke/semiologien-meiner-künstlichen-welt.html">Werke</a>
        <ul id="nav_desktop_werke">
          <li>
            <a href="/werke/semiologien-meiner-k%C3%BCnstlichen-welt.html">Semiologien meiner künstlichen Welt</a>
          </li>
        </ul>
      <li class="menu-item-vita">
        <a href="/vita.html">Vita</a>
      <li class="menu-item-impressum/datenschutz">
        <a href="/impressum.html">Impressum/Datenschutz</a>
    </ul>
  </nav>

I tried it with

<nav id="nav_desktop" class="hidden">
  <h1 class="logo"><a href="/">Fabian Widukind <br>Penzkofer</a></h1>
  <ul>
    {% for item in site.menus.header %}
    {%- if page.url contains "werke" -%}
    <li class="menu-item-{{ item.title | downcase }}">
      <a href="{{ item.url }}" {% if page.url == item.url %}class="current" {% endif %}>{{ item.title }}</a>
      <ul id="nav_desktop_werke">
        {%- for werk in sortedWerke -%}
        <li>
          <a href="{{ werk.url }}" {% if page.url contains werk.url %}class="current" {% endif %}>{{ werk.title }}</a>
        </li>
      </ul>
        {%- endfor -%}

      {%- else -%}
    <li class="menu-item-{{ item.title | downcase }}">
      <a href="{{ item.url }}" {% if page.url == item.url %}class="current" {% endif %}>{{ item.title }}</a>
    </li>


    {%- endif -%}
    {%- endfor -%}
  </ul>
</nav>

unfortunately I get something like

<nav id="nav_desktop" class="hidden">
      <h1 class="logo"><a href="/">Fabian Widukind <br>Penzkofer</a></h1>
      <ul>
        <li class="menu-item-jetzt">
          <a href="/">Jetzt</a>
          <ul id="nav_desktop_werke">
            <li>
              <a href="/werke/semiologien-meiner-k%C3%BCnstlichen-welt.html">Semiologien meiner künstlichen Welt</a>
            </li>
          </ul>
        <li class="menu-item-werke">
          <a href="/werke/semiologien-meiner-künstlichen-welt.html">Werke</a>
          <ul id="nav_desktop_werke">
            <li>
              <a href="/werke/semiologien-meiner-k%C3%BCnstlichen-welt.html">Semiologien meiner künstlichen Welt</a>
            </li>
          </ul>
        <li class="menu-item-vita">
          <a href="/vita.html">Vita</a>
          <ul id="nav_desktop_werke">
            <li>
              <a href="/werke/semiologien-meiner-k%C3%BCnstlichen-welt.html">Semiologien meiner künstlichen Welt</a>
            </li>
          </ul>
        <li class="menu-item-impressum/datenschutz">
          <a href="/impressum.html">Impressum/Datenschutz</a>
          <ul id="nav_desktop_werke">
            <li>
              <a href="/werke/semiologien-meiner-k%C3%BCnstlichen-welt.html">Semiologien meiner künstlichen Welt</a>
            </li>
          </ul>
      </ul>
    </nav>

Any Ideas?

Thanks
Seba

I think you have the endif to far down, it should be above the else I think. Try it with it between that first endfor and the else.

well, that is what the else is for. Hold that thought!

little hard to tell with this example and no access to the menus, I would make a much simpler example to see what is going on.

Should if page.url really be if item.url? that would make sense if page.url is the page you put this on and it has werke in it, then that would always be true and the same thing would repeat how ever many times there are a menu item.

All right, I’m a little confused, where is sortedweke defined? is there more?

I’d strip out everything and just out put the menu stuff without even any html to see what it is doing, this is a little too hard to read for me.