KaKi87
April 25, 2024, 12:51am
1
Hi,
How to do this conditionally though ?
I tried adding an is_external
boolean, but the IDE says Schema validation: Property 'is_external' is not allowed
and the terminal command outputs can't evaluate field is_external in type *navigation.MenuEntry
Thanks
There are a couple of ways to do this.
1) Open all external URLs in new tab
{{ with site.Menus.main }}
<nav class="menu">
{{ range . }}
{{ if (urls.Parse .URL).IsAbs }}
<a href="{{ .URL }}" rel="external" target="_blank">{{ .Name }}</a>
{{ else }}
<a href="{{ .URL }}">{{ .Name }}</a>
{{ end }}
{{ end }}
</nav>
{{ end }}
2) Configure each menu entry as needed
menus:
main:
- name: Home
pageRef: /
weight: 10
- name: Posts
pageRef: /posts
weight: 20
- name: Hugo
url: https://gohugo.io/
weight: 30
params:
external: true
{{ with site.Menus.main }}
<nav class="menu">
{{ range . }}
{{ if .Params.external }}
<a href="{{ .URL }}" rel="external" target="_blank">{{ .Name }}</a>
{{ else }}
<a href="{{ .URL }}">{{ .Name }}</a>
{{ end }}
{{ end }}
</nav>
{{ end }}