[SOLVED] Listing Pages by Param in a Dropdown Menu Site Wide

Hello everyone, it me once again :confused:
A new day, a new problem and i really hope i didn’t overlook something simple onece again.
My Problem is that my header.html which includes my navbar doesn’t render my dropdown when im on a page which uses the single.html template.
It works fine on my index.html and i dont understand why it wouldn’t render.
Here is my Code:

{{ range $.Site.Pages }}
                {{ if .Params.navbar }}
                  {{ if  .Params.dropdown }}
                    <div class="dropdown">
                      {{ if $.Params.clickable }}
                        <li class="navbtn"><a href="{{ .URL }}/">{{ .Name }}</a></li>
                      {{ else }}
                        <li class="navbtn"><a href="#">{{ .Name }}</a></li>
                      {{ end }}
                      <div class="dropdown-content">
                          {{ range .Params.dropdown }}
                            <div class="col-md-6">
                              {{ range where $.Pages "Section" . }}
                                <a href="{{.RelPermalink}}">{{ .Title }}</a>
                              {{ end }}
                          {{ end }}                        
                  {{ else  }}
                    <li class="navbtn"><a href="{{ .URL }}/">{{ .Name }}</a></li>
                  {{ end }}
                {{ end }}
              {{ end }}

It renders everything but the part in the {{ range where .Pages "Section" . }}. The only thing i can think of is that im ine the wrong scope but shouldn't the get me to the top level again?.
Thanks in adavance :slight_smile:

The above function is for list pages only.

You could try:

  1. Store that range in a variable with Scratch so that you can call it in other contexts such as single pages
  2. Use .GetPage to fetch these pages (works in both lists and single pages).

I think that .GetPage is probably what you want to use.

i tried to solve it with .GetPage but it seems like i cant range over the .Get.Page.
That is the error i get:

ERROR 2018/08/23 13:26:55 Error while rendering “taxonomyTerm” in “”: template: _default/list.html:9:4: executing “_default/list.html” at <partial "header.html…>: error calling partial: template: partials/header.html:36:49: executing “partials/header.html” at <.>: range can’t iterate over {0xc04201dd80 0xc042a767e0 section Pages(2) [] [] [0xc042a11400] map[] [] [] [] false false {0 0 0 0} false [] [] [] false [] map[] { } [Dienstleistungen] 0xc042a10a00 [] 0xc042014600 Site(“Airspace for Hugo”) Dienstleistungens [] map[Pages:[0xc042312500 0xc04235a000]] {2018-02-02 13:07:31 +0200 +0200 2018-02-02 13:07:31 +0200 +0200 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC} { 0 } { } //localhost:1313/dienstleistungen/ /dienstleistungen/ dienstleistungen/ false {Dienstleistungen Dienstleistungen section en false} 0xc04218c1c0 [{HTML {text html . [html] } index canonical false true false false} {RSS {application rss xml . [xml] } index alternate false false true false}] 0xc042a89400 0xc0421c14a0}

From what I can make out of the Console Error, it seems that you are not using the current syntax of .GetPage

This function has been rewritten recently see here:

Also posting a link to your repository would help pinpoint the issue.

That is the syntax im using:

{{ range .Params.dropdown }}
      <div class="col-md-6">
       {{ range $.Site.GetPage . }}
              <a href="{{.RelPermalink}}">{{ .Title }}</a>
        {{ end }}
 {{ end }}     

Should have linked to my respo in the first place.

You are not specifying the PATH of a page (or a set of pages) with the above hence the stack trace error in the console.

Please see the Doc I linked to above.

Also if you want to grab section pages you could also do something like the following:

{{ $sections := .Site.GetPage "section" .Section }}

And then use {{ range $sections }}

I’ve read the page you linked already but im still not understanding it.
In there it sais that this is the syntax: .Site.GetPage “/blog”
But because im already within a range

{{ range .Params.dropdown }}

I want to have it dynamic so that it ranges over my .dropdown with is an array with the names of the folders that i want to have shown.

$.Site.GetPage .

With my limited understanding this should go to the root level and grab the pages that are in my Dot. I have 2 Folders with i specified in the .dropdown array so i want to have:

 $.Site.GetPage "Dienstleistungen"
 $.Site.GetPage "Produkte"

It doesn’t give me an error if i use

{{ with $.Site.GetPage . }}

Thats why i think that it just wont work range over it.
If i use with it only shows me the first entry of my array and puts an “s” behind the title and shows me a listpage which shows me my single pages within the folde rinstead of just the single pages.

Ok. I had a better look of your repository and I understood what you’re trying to do.

You simply need to change line 36 of your header.html partial to:
{{ range where $.Site.Pages "Section" . }}

Basically you were missing the .Pages context because you were calling this variable from within the range of your dropdown parameter.

Always remember that when encountering such errors the first thing you should try is to go higher up.

See the Docs about the .Pages variable.

P.S. I renamed the title of the thread to something more appropriate.


Thank you for being so patient :slight_smile:
I got it to work by using this:

    {{ with $.GetPage .}}
       {{ range .Pages }}
          <a href="{{.RelPermalink}}">{{ .Title }}</a>
         {{ end }}
     {{ end}}

But your solution is way cleaner :slight_smile:
Thank you very much :slight_smile:

1 Like