How to pass context to a shortcode?

I am exploring this example git clone --single-branch -b hugo-forum-topic-40858 https://github.com/jmooring/hugo-testing hugo-forum-topic-40858. What’s the correct way to pass the context in a shortcode so that the pages $p in the same branch bundle are shown by the shortcode when the code is added to it?

{{ $p := append .CurrentSection.Pages.ByWeight (slice .CurrentSection) }}
{{- with $p.Next . }}
{{- end }}

Page templates (baseof, page, section, taxonomy, term, home) receive the current page in context. Or to put it another way, the dot (.) in a page template is a Page object, allowing to do this:

.CurrentSection.Pages.ByWeight

Unlike page templates, the dot in a shortcode template does not represent the current page. To access the current page within a shortcode template you must do:

.Page.CurrentSection.Pages.ByWeight

See Shortcode methods.

1 Like

I actually did that before but an error kept popping up execute of template failed at <.>: wrong type for value; expected page.Page; got *hugolib.ShortcodeWithPage. It seems {{- with $p.Next . }} should be {{- with $p.Next .Page }}. (I also noticed I need to add .Page before every variable).

Do you still have a question, or have you solved the problem?

I was just confused coz I spent hours debugging that error.

1 Like

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