Use page parameter in included headless snippet

Hi,

I’d like to include a text snippet from a headless bundle in multiple different content pages, and would like to use a parameter from the content page in the headless snippet, so it gets resolved to a different value on the different content pages. That way I need to update the snippet only in the headless bundle, but still can have minor differences in the content pages (for example, the name of a product).

To make it a bit clearer:

  content
    headless
      snippet.md 
    product1
      product1.md
    product2
      product2.md

The idea is that produc1.md and product2.md have a parameter in their frontmatter (productname: Product1, productname: Product2), and use a simple shortcode to include the snippet.md file.
{{< include "headless/snippet.md" >}}

snippet.md has some text and references the productname parameter, something like:
This product is called {{< param ".Page.Params.productname" >}}

The expected output is:

  product1.html
    This product is called Product1

  product2.html
    This product is called Product2

However, I couldn’t get it work (when Hugo processes the snippet file, it complains about the missing productname parameter):
Building sites … ERROR 2020/06/05 06:58:19 Param ".Page.Params.productname" not found: "/src/content/headless/doc/snippet.md:5:201

Has anyone got something like this working in Hugo?

Kind Regards,
Robert

The errors are text, you should include them when posting to #support.

Sounds like you are calling the shortcode in a content that doesn’t have the param for productname.

Hi, I’ve added the error message to the original post.

I do define the productname parameter in the frontmatters of the product1/2.md files, but get the error despite that. It looks like Hugo tries to process the included content before including it into the product pages.

I’ve tried to experiment with using .Content and .RawContent in the include shortcode, and changing the < to % where I call the shortcode, but couldn’t find a combination that gives the desired output.

Please share your repo or a reproducible repo with your issue. I was going to ask for the contents of snippets.md, but I don’t like asking a new question each time. If we can reproduce your error, we can fix it a lot faster. :slight_smile:

Hi maiki,
Thanks a lot for your help, you can find a minimal repo at https://github.com/fekete-robert/hugo-headless-parameter-problem

Hi @fekete-robert, are you able to solve this? I am trying to achieve the same expected output as you

As a test, I ran your repo on my local and changed the snippet.md file like below and there’s no error,

./content/headless/snippet.md

---
productname: Product3
---
This product is called {{< param "productname" >}}

but it did not give a correct expected output

  product1.html
    This product is called Product3

  product2.html
    This product is called Product3

It seems this param shortcode read the param from the snippet file (headless page) instead of the front-matter params in the page where its parent’s shortcode is located

the source for this built-in param shortcode (ref):

.../shortcodes/param.html

{{- $name := (.Get 0) -}}
{{- with $name -}}
  {{- with ($.Page.Param .) }}
    {{ . }}
  {{ else }}
    {{ errorf "Param %q not found: %s" $name $.Position }}
  {{ end -}}
{{- else }}
  {{ errorf "Missing param key: %s" $.Position }}
{{ end -}}

Hi, unfortunately I couldn’t get it work, so I don’t use page parameters in snippets.