Cascading list objects

So I am having trouble cascading complex objects.

Simple key-value cascade great.

But if i try to cascade:

[[cascade]]
  [cascade.params]
    [[list_of_values]]
     id = "1"
     name = "abc"

    [[list_of_values]]
      id  = '2'
      name = "test"

Nothing shows up in the downstream pages.

On the page I can also define

[params]
  [[list_of_values]]
     id = "1"
     name = "over-ride-abc"

And that works fine. I was hoping to populate a bunch of the values via cascade, and then overwrite, just some values as needed per page.

Is this possible?

This should be

[cascade]
[[cascade.params.list_of_values]]
id = "1"
name = "abc"

[[cascade.params.list_of_values]]
id = "2"
name = "test"

In other words, list_of_values is a key in the params map whose value is a slice. In your article, you can access the entire slice as params.list_of_values and individual elements with the index function

Thanks,

Unfortunately if i try to set a value in the map it overwrites the whole map.

so

[params]
  [[list_of_values]]
     id = "1"
     name = "over-ride-abc"

Then is the whole map and id 2 is dropped. Anyway around that?

Yes - it would.
If you could explain what you are aimimg for with some context, I am sure someone would be able to help you here. If you want to override individual elements of the slice, one option I can think of is to

  1. Add your default slice of map (what you are cascading currently) in the .Data section of the site
  2. Add the overrides as params in the document
  3. Implement the specifics of your override logic in the template by comparing the override against the default Data while rendering

just out of the bat using maps instead of lists and merge

  • Default Values: _index.md

    [cascade]
      [cascade.params.defaultValues.1]
        id = "1"
        name = "abc"
      [cascade.params.defaultValues.2]
        id = "2"
        name = "test"
    
  • Page values: page.md

    [params.pageValues.1]
      id = "1"
      name = "out-of-the-bat"
    
  • layout: single.html

    {{/* make sure both are valid maps*/}}
    {{ $default := or .Params.defaultValues dict }}
    {{ $page := or .Params.pageValues dict }}
    
    {{ /* merge the two maps (order matters) and use sort to remove the keys */}}
    {{ $listValues := sort (merge $default $page) }}
    
    {{/* display the Values */}}
    {{ highlight ($listValues | jsonify (dict "indent" "  ")) "JSON" }}
    
  • Result:

    [
      {
        "id": "1",
        "name": "out-of-the-bat"
      },
      {
        "id": "2",
        "name": "test"
      }
    ]
    
2 Likes

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