Variable scope

I have a partial cta-footer.html and another partial cta.html

Sometimes, cta.html is called on its own, and sometimes, I call cta-footer.html, which contains cta.html.

Inside cta-footer.html, I have:
{{ partial "service/cta.html" (dict "context" . "position" "bottom") }}

Inside cta.html, I have:
<a id='{{ .position | default "top" }}'>Link</a>

When I try to render the website, all the pages where cta.html is called from within cta-footer.html work fine, but it cashes when it tries to generate pages where cta.html is called on its own.
Error is:
at <.position>: can't evaluate field position in type *hugolib.PageOutput

My thinking is that inside cta.html, if .position has been set by partial “service/button-cta.html” …, then I use its value, else I just use “top”. However, I can’t get it to work.


Does it work if your wrap your line with an if? So:

{{ if .position }}
  <a id='{{ .position }}'>Link</a>
{{ else }}
  <a id='top'>Link</a>
{{ end }}

I get the same error.

Can we see how you call the cta.html partial on its own compared to how it looks from inside cta-footer.html ?

Like this:
{{ partial "service/cta.html" . }}

You have to use the same dict as parameter. Otherwise the partial thinks .product is a key of the context that you passed : the page context.