Did something change with .GetPage in recent updates?

When I encountered .GetPage initially, it was through a tutorial that had it as .Site.GetPage (which I replaced with site.GetPage). So, I thought it was necessary to add site before .GetPage in every instance which I did. A few days ago, I saw a reply somewhere in the forum with .GetPage used without site. Is there a difference between the two? I can only get .GetPage to work in single.html and it throws an error in other templates.

So page.GetPage is simply .GetPage on single.html?

Well page is not affected by context, while .Page is. So in a range .Page.GetPage will be in the context of the . (the page in the range) while page will be the calling page.

1 Like

My initial question was why .GetPage fails in any other template than single.html? In those templates, it needs site before it?

There are two methods, one on .Page and one on .Site.

As @razon described, .Page.GetPage will first try to resolve the given path relative to the current page.

And be careful how you use the page function.

1 Like

What error do you see, and what templates? Can you show a repo or a repo with a reproducible example?

Without specific code it’s difficult to diagnose or know what is going on.

layouts/partials/test.html (used in index.html).

 execute of template failed at <.GetPage>: can’t evaluate field GetPage in type string
{{- $section := index site.Params.mainSections 0 }}
{{- with index site.Params.nestedSections 1 }}
  {{- with .GetPage (printf "%s/%s" $section . ) }}
<!-- rest of the code here -->
{{- end }}
{{- end }}
[Params]
mainSections = ["lorem","ipsum"l
nestedSections = ["veni","vidi","vici"]

When you do this the . (context) become vidi, and not the page. You need to either save the . and get page on that variable, or use page.

For example (without page):

$pctx := .
{{- with index site.Params.nestedSections 1 }}
  {{- with $pctx.GetPage (printf "%s/%s" $section . ) }}
<!-- rest of the code here -->
{{- end }}
{{- end }}

HTH

See also:

So, I needed to declare the dot outside the scope. Got it. For now, I will stick with site.

1 Like

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