Draft menu entries

I’m currently looking into the completeness of the draft mode: According to Menus there is no flag to indicate that a menu entry shod be only generated in draft mode. Is the documentation complete or is this a missing feature?

I didn’t try but would like to know if this would be feature other would like to see as well. For now I solved it myself by using the following in the menu entry:

params:
      draft: true

And then in the template:

      {{- with .Site.Menus.main -}}
        {{- range sort . -}}
          {{- if and .Params.draft (not hugo.IsDevelopment) -}}
            {{- continue -}}
          {{- end -}}
        {{- end -}}
     {{- end -}}

But this is a workaround since this hits another missing feature, the seems to be no method to check for draft mode?!

That can’t be right, I must be missing something…

1 Like

Draft is a page attribute and pages where this is true will be ignored unless you use --buildDrafts.

so as you guessed you will have to implement that yourself.

this works fine for me using a site generated with hug new theme --themesDir . mysite

adjusted menu entry for Tags
[menus]
   [[menus.main]]
      name    = 'Tags'
      pageRef = '/tags'
      weight  = 30
      [menus.main.params]
         draft = true```

maybe check your YAML indentation/ Syntax

menus:
  main:
    - name: Tags
      pageRef: /tags
      weight: 30
      params:
        draft: true
menu template
<nav>
   <ul>
      {{- with site.Menus.main }}
         {{- range sort . -}}
            {{- if .Params.draft -}}
               {{- continue -}}
            {{- end -}}
            <li>{{ .Name }}</li>
         {{- end -}}
      {{- end -}}
   </ul>
</nav>

the Tags menu entry is not rendered

check your context .

1 Like

@irkode Thanks, but that doesn’t really address my points. My solution is already working, but it’s not what one might expect.

The first question: Might it be a worthwhile addition to add a draft mode to menu entries as well?
The second question (and you example falls short in this regard): How does one trigger the .Params.draft when in draft mode? I used (not hugo.IsDevelopment) which in general matches the behaviour I expect but is just another work around, since one could also enter development mode without having the drafts build.

Is there no way to check in the template if we are in draft mode?

1 Like

oops, you’re right have been on the wrong track. sry

to check if buildDrafts is active we have:

That’s not a question for me to answer, but never heard such a request before.

usually one defines a menu or not maybe more details on the Use case will help to understand the need.

1 Like

@irkode Great, Thanks, that was actually what I was looking for. I was just looking at the wrong namespace, certainly because I was under the assumption, that it’s a command line setting like the environment ans those are under the hugo prefix / namespace (Hugo functions).

Since menu Item are just links to existing content it shouldn’t be suprising that one might want to hide an entry. Just because the target could be missing. I guess it would be overkill to check agains the link target and hide an menu item when the target isn’t visible. On the other hand this would allow to hide menu items that have a target with a build date in the future. :wink:

Thanks for your help!

1 Like

When the menu entry specifies a pageRef, Hugo attempts to find a published page with the corresponding logical path. Draft, future, and expired content is not published, so the menu entry’s .Page method returns nil.

So you can exclude menu entries by wrapping the rendering code in a conditional. For example:

{{ with site.Menus.main }}
  <nav class="nav-inline menu">
    <ul>
      {{ range . }}
        {{ if .Page }} <---------------- begin conditional
          {{ if $.IsMenuCurrent .Menu . }}
            <li class="active"><a aria-current="page" href="{{ .URL }}">{{ .Name }}</a></li>
          {{ else if $.HasMenuCurrent .Menu . }}
            <li class="ancestor"><a aria-current="true" href="{{ .URL }}">{{ .Name }}</a></li>
          {{ else }}
            <li><a href="{{ .URL }}">{{ .Name }}</a></li>
          {{ end }}
        {{ end }} <---------------- end conditional
      {{ end }}
    </ul>
  </nav>
{{ end }}

You could also check for .URL, which will be an empty string if the page does not exist.

1 Like