How to make GetPage throw an error if page not found

Dear all,

we do something like this:

{{ ($.Site.GetPage "/foo/bar/index.md").Permalink }}

in templates to address content pages from templates.

When the page is not there Hugo sets a link to the startpage.

We would prefer that Hugo throws an “not found” error. How can we achieve this?

You can do this:

{{ with site.GetPage "foo" }}
    {{ .Permalink }}
{{ else }}
    {{ errorf "foo page was not found" }}
{{ end }}

You can wrap something like that in a shortcode if you plan to use that from your content pages.

Kind of related (see the source code of the titleref shortcode I describe in that post):

5 Likes

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