Search code examples
hugohugo-shortcode

How can I use Hugo's param shortcode to access a value in a nested list?


I'm trying to use Hugo's baked in param shortcode in my markdown file, but I can't figure out what the syntax should be for a nested dict.

For example, if I have the following in my markdown file, I get a Param "nestedList.[1].value" not found error

---
title: "My Hugo Post"
date: 2023-11-22
nestedList:
  - name: "Nested Item 1"
    value: 10
  - name: "Nested Item 2"
    value: 20
---

I can display the title just fine with: {{< param "title" >}}.

But I can't display the value of the second nested list item ("20") with {{< param "nestedList.[1].value">}}

How can I display the value for the second value key in the nested dict?

Hugo's documentation for the param shortcode isn't helping me, unfortunately.


Solution

  • I doubt if this is possible.

    You CAN achieve this by creating a custom shortcode called 'nestedList'. That shortcode can be called like this:

    {{< nestedList 2 >}}

    In that shortcode you can get the second item using:

    {{ (index $.Page.Params.nestedList (.Get 0)).value }}