Current page properties are set incorrectly

My navigation and URL references go nuts – and I can’t see the pattern which might need fixing.
Here is the navigation template:

 <div class="d-none d-md-flex mr-2">
	<ul class="nav-main">
		{{ $currentPage := . }}
		{{- range .Site.Menus.main -}}
		<li class="{{ if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }}selected{{ end }} {{ if .HasChildren }}dropdown{{end}}">
			<a {{- if gt (len .Pre) 0 }}class="{{.Pre}}"{{ end }} href="{{ .URL | absLangURL }}">{{ .Name | markdownify }}</a>
			{{ if .HasChildren }}
				<ul class="sub">
					{{ range .Children }}
						<li class="{{ if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }}selected{{ end }}">
							<a href="{{ .URL | absLangURL }}">
								{{ $icon := (.Pre) }}
								{{ if $icon }}
								<i class="nav-deco nav-deco--{{ .PageRef }} ico {{$icon}}"></i>
								{{ end }}
								<div>
									<span class="title">{{ .Name | markdownify }}</span>
									<span class="description">{{ .Post }}</span>
								</div>
							</a></li>
					{{ end }}
					{{ if eq .PageRef "industries" }}
					<li>
						<a href="{{.URL | absLangURL }}">
							<div>
								<span class="title">{{ i18n "moreIndustriesNavi"}}</span>
							</div>
						</a>
					</li>
					{{ end }}
				</ul>
			{{ end }}
		</li>
		{{ end -}}
	</ul>
</div>

Here is the entry in the config.toml for the pages in question:

# ENGLISH

[languages]
[languages.en]
	weight = 1
	title = "Shift scheduling and team management – smart, easy, free"
	languageName = "English"

[languages.en.params]
	langcode = "English"
	lang = "en"

[[languages.en.menu.main]]
	name =  "Product"
	weight = 1
	url = "/product"
	pageref = "product"

[[languages.en.menu.main]]
	name = "Scheduling"
	weight = 10
	url = "/shift-planner/"
	pageref = "shift-planner"
	parent = "Product"
	pre = "i-calendar"
	post = "Some description"

[[languages.en.menu.main]]
	name = "Management"
	weight = 12
	url = "/manager-app/"
	pageref = "manager-app"
	parent = "Product"
	pre = "i-team"
	post = "Some description"

[[languages.en.menu.main]]
	name = "Time tracking"
	weight = 13
	url = "/time-tracking-attendance/"
	pageref = "time-tracking"
	parent = "Product"
	pre = "i-punch-in"
	post = "Some description"
	
[[languages.en.menu.main]]
	name = "Payroll"
	weight = 14
	url = "/payroll/"
	pageref = "payroll"
	parent = "Product"
	pre = "i-wallet"
	post = "Some description"

[[languages.en.menu.main]]
	name = "Industries"
	weight = 2
	url = "/industries/"
	pageref = "industries"

[[languages.en.menu.main]]
	name = "Retail & Shops"
	weight = 22
	url = "/staff-scheduling-retail/"
	pageref = "industry-retail"
	post = "Some description"
	parent = "Industries"

[[languages.en.menu.main]]
	name = "Healthcare & Medical"
	weight = 23
	url = "/staff-scheduling-pharmacy/"
	pageref = "industry-pharmacy"
	post = "Some description"
	parent = "Industries"

[[languages.en.menu.main]]
	name = "Catering & Gastro"
	weight = 24
	url = "/staff-scheduling-catering/"
	pageref = "industry-gastro"
	post = "Some description"
	parent = "Industries"

[[languages.en.menu.main]]
	name = "Hotels & Hospitality"
	weight = 25
	url = "/staff-scheduling-hotels/"
	pageref = "industry-hotels"
	post = "Some description"
	parent = "Industries"

[[languages.en.menu.main]]
	name = "Security & Surveillance"
	weight = 26
	url = "/industry-security/"
	pageref = "industry-security-guards"
	post = "Some description"
	parent = "Industries"

[[languages.en.menu.main]]
	name = "Pricing"
	weight = 8
	url = "/pricing/"
	pageref = "pricing"

[[languages.en.menu.main]]
	name = "Resources"
	weight = 7
	url = "/resources/"
	pageref = "resources"

[[languages.en.menu.main]]
	name = "Case studies"
	weight = 52
	url = "/case-studies/"
	pageref = "case-studies"
	pre = "i-star ico--color"
	post = "qwert"
	parent = "Resources"

[[languages.en.menu.main]]
	name = "Blog"
	weight = 53
	url = "/blog/"
	pageref = "blog"
	pre = "i-speech ico--color"
	post = "qwert"
	parent = "Resources"

[[languages.en.menu.main]]
	name = "Excel templates"
	weight = 54
	url = "/resources/excel-templates/"
	pageref = "excel"
	pre = "i-template ico--color"
	post = "qwert"
	parent = "Resources"

[[languages.en.menu.main]]
	name = "Business days"
	weight = 55
	url = "/resources/business-days/"
	pageref = "biz-days"
	pre = "i-holiday ico--color"
	post = "qwert"
	parent = "Resources"

here is the _index.en.md for the Industry page:

---
title: "Page Title"
keywords: "page keywords"
description: "page seo descriptions"
date: 2017-11-21T21:28:43+01:00
draft: false
layout: single
---

I run the local server with $ hugo serve -w --disableFastRender --ignoreCache

When I point my browser to /indusrties URL it most of the time(!) shows me the resources page instead (as content AND selected in the navigation).
But when I go to sub pages under Industry navigation, it shows them alright, correctly highlighting “Industry” in the navigation.

So I decided to debug what I can. In the navigation template I inserted {{ jsonify $currentPage }} and this is what I’ve got for the http://localhost:1313/industries url in the browser:

{
  "Date": "2022-03-30T14:28:52+01:00",
  "Lastmod": "2022-03-30T14:28:52+01:00",
  "PublishDate": "2022-03-30T14:28:52+01:00",
  "ExpiryDate": "0001-01-01T00:00:00Z",
  "Aliases": null,
  "BundleType": "",
  "Description": "",
  "Draft": false,
  "IsHome": false,
  "Keywords": null,
  "Kind": "page",
  "Layout": "",
  "LinkTitle": "Industries",
  "IsNode": false,
  "IsPage": true,
  "Path": "/resources/list/industries", <-- WTF?
  "Slug": "",
  "Lang": "en",
  "IsSection": false,
  "Section": "resources",
  "Sitemap": {
    "ChangeFreq": "",
    "Priority": -1,
    "Filename": "sitemap.xml",
    "Disable": false
  },
  "Type": "resources",
  "Weight": 2
}

Interestingly enough, sometimes(!!) I am lucky and get the content what I want to see (most of the time after changing something within that page content or templates) – but then it is not selected in the navigation anyway.

And ideas?

As a first guess I think you call the navigation with partialCached … which would explain the non deterministic behaviour.

Here is the baseof.html under theme/layouts/_default

		<!-- navigation -->
		{{- block "navigation" . -}}
		{{- end -}}
		<!-- main -->
		{{- block "main" . -}}
		{{- end -}}
		<!-- footer -->
		{{- block "footer" . -}}
			{{ partial "footer" . }}
			{{ partial "cookies" . }}
		{{- end -}}

and here is the theme/layouts/resources/single.html

{{ define "navigation" }}
	{{ partial "navigation" . }}
{{ end }}

{{ define "main" }}

	{{ partial "resources/hero" . }}
	{{ partial "resources/intro" . }}
	{{ partial "resources/list" . }}

{{ end }}

This part

{{ define "navigation" }}
	{{ partial "navigation" . }}
{{ end }}

is the same in every single.html, list.html and other template files for the pages.

Actually, come think of it, in the previous version of the site, the industries section was part of the resources section, but now I moved it out (see navigation in config.toml). Where else could I look for the trouble?

General comments:

1) When defining a menu entry, specify pageRef for internal links and url for external links. Do not specify both.

2) Do not use the .PageRef method in a menu template. Yes, it returns the pageRef menu entry property verbatim, but the method is not documented and will probably be removed in a future release. If it’s not documented, don’t use it.

3) As with the issue we worked on yesterday, this would be more efficient to troubleshoot with a complete example.

Ok, one question before I share the repo: where does the .Path property come from? I see it when I dump jsonified dot context in navigation.html ($currentPage), but the value of that property is nowhere to be found. It seems like it is being generated by hugo. When I search for the full string or event the part of the string, it is not set in either front matter, config or elsewhere. What is it?

Ok, right, the path contains the name of the file. Weird. I’ll investigate further.

Please fix your menu entries before sharing your repo. This doesn’t make sense to me:

	url = "/staff-scheduling-retail/"
	pageref = "industry-retail"

Now after deleting pageref properties none of my navigation items gets highlighted

{{ $currentPage := . }}
{{- range .Site.Menus.main -}}
<li class="{{ if or ($currentPage.IsMenuCurrent "main" .) ($currentPage.HasMenuCurrent "main" .) }}selected{{ end }} {{ if .HasChildren }}dropdown{{end}}">
...

You did the opposite.

Try this:

git clone --single-branch -b hugo-forum-topic-49919 https://github.com/jmooring/hugo-testing hugo-forum-topic-49919
cd hugo-forum-topic-49919
hugo server

While I’ve cleaned up the config and now have it only with pageRefs referencing the content structure, so that for

content
|-- industries
    |--_index.en.md
    |--_index.de.md
    |--_index.fr.md

it looks like this:

[[languages.en.menu.main]]
	name = "Industries"
	weight = 2
	pageRef = "/industries"
	[languages.en.menu.main.params]
		deco = "industries"

...

[[languages.de.menu.main]]
	name = "Branchen"
	weight = 2
	pageRef = "/industries"
	[languages.de.menu.main.params]
		deco = "industries"

removed all aliases in the front matter of _index.lang.md files and put the URLs in there like this, ie. _index.en.md:

---
title: "Title."
date: 2017-11-21T21:28:43+01:00
draft: false
layout: single
url: industries
---

and _index.de.md

---
title: "Titel."
date: 2017-11-21T21:28:43+01:00
draft: false
layout: single
url: branchen
---

I still can’t get it work consistently. As soon as I change something in navigation template it rewrites the inner pages and industries ends up under resources and deep links like resources/templates are returning the Resources page itself. It seems to me like it is a caching issue? The page Industries used to be placed under resources before, but not anymore.

I run the server with $ hugo serve -w --disableFastRender --ignoreCache but it doesn’t seem to help. How do I kill hugo cache on the file system?

Does the example site from my earlier reply behave as you expect?

Almost, but not with the inner pages, which you omitted :slight_smile:

The definition is based on what you provided in your initial post.

image

I think I’ve found the issue. It was a URL param in the front matter of pages, which I need to rename to avoid confusion.

My previous guidance about using url and pageRef in menu entries is important, otherwise HasMenuCurrent and IsMenuCurrent will not work.

Hi jmooring, thanks you gived good example. But there I had meet a strange trouble about the menu entries. Here is my menus settings in configuration file like below:

menus:
  main:
    - identifier: home
      name: 首页
      pageRef: /
      pre: home
      weight: 1
    - identifier: about
      name: 关于
      pageRef: /about.html
      pre: user
      weight: 2
    - identifier: flinks
      name: 友情链接
      pageRef: /flinks.html
      pre: thumbs-up
      weight: 3
    - identifier: example
      name: 语法示例
      pre: angles-down
      weight: 4
    - identifier: flow-charts
      name: 流程图
      pageRef: /demo/mermaid-charts.html
      weight: 1
      parent: example
    - identifier: math
      name: 数学公式
      pageRef: /demo/math-formula.html
      weight: 2   
      parent: example
      parent: example
    - identifier: syntax
      name: 语法高亮
      pageRef: /demo/syntax-highlighting.html
      weight: 3   
      parent: example
    - identifier: archives
      name: 归档
      pageRef: /archives
      pre: archive
      weight: 5
    - identifier: commonweal
      name: 公益 404
      pageRef: /404.html
      pre: heartbeat
      weight: 6

And then I write a menus.html to display the menus with this code:

{{ $ms := .Site.Params.menuSets }}
{{ $curP := .Page }}
<nav class="site-nav">
  <ul class="main-menu menu">    
  {{ range .Site.Menus.main }}
    {{ $active := $curP.IsMenuCurrent .Menu . }}
    <li class="menu-item menu-item-{{ .Identifier }}">
      <a href="{{ .URL | relLangURL }}" class="{{ if .HasChildren }}menus-parent{{ end }} hvr-icon-pulse {{if $active }} menu-item-active{{ end }}" rel="section">
        {{ if $ms.icons }}<i class="fa fa-{{ .Pre }} hvr-icon"></i>{{ end }}{{ .Name }}
        {{ if .HasChildren }}<span class="menu-item-shrink-icon"><i class="fa fa-angle-right"></i></span>{{ end }}
        {{ if and $ms.badges (eq .Identifier "archives") }}
          <span class="badge">{{ $.Scratch.Get "postsCount" }}</span>
        {{ end }}
      </a>
      {{ if .HasChildren }}
      <ul class="menu-children">
      {{ range .Children }}
      {{ $active = or ($curP.IsMenuCurrent .Menu .) ($curP.HasMenuCurrent .Menu .) }}
        <li class="menu-child-item">
          <a href="{{ .PageRef | relLangURL }}" class="hvr-icon-pulse {{if $active }}menu-item-active{{ end }}" rel="section">
            <i class="fa hvr-icon"></i>{{ .Name }}
          </a>
        </li>
      {{ end }}
      </ul>
      {{ end }}
    </li>
  {{ end }}    

  {{ if or .Site.Params.algoliaSearch.enable .Site.Params.localSearch.enable }}
    <li class="menu-item menu-item-search">
      <a role="button" class="popup-trigger hvr-icon-pulse">
        {{ if $ms.icons }}<i class="fa fa-search fa-fw hvr-icon"></i>{{ end }}{{ T "Sidebar.label.search" }}
      </a>
    </li>
  {{ end }}
  </ul>
</nav>

But there not happed what I want, it has 2 problems as :

  1. Range .Children can not use the .URL method , it will print null value, so there use the .PageRef got the right menu link .
  2. or ($curP.IsMenuCurrent .Menu .) ($curP.HasMenuCurrent .Menu .) this method not work in children menu.

I had read your test code but not found other special settings , your code not meet 2 problems as above, there so interesting. So what’s wrong with me ?

Thanks.

https://gohugo.io/content-management/menus/#properties-site-configuration

pageRef
(string) The logical path of the target page, relative to the content directory. Omit language code and file extension. Required for internal links.

https://gohugo.io/methods/menu-entry/pageref/

The use case for this method is rare.
In almost also scenarios you should use the URL method instead.

1 Like

Thanks very much.

I have understood the usage of the pageRef parameter. And change it reference the logical path of article inside, then all things is run well and right.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.