HUGO

[Solved]Delimit map from data file

To start, in data/products, I have product.toml with the a map for color values like this:

[color]
[[color.logo]]
name = "blue"
cid = "blue"
images = "http://placehold.it/330x508"

[[color.logo]]
name = "Green"
cid = "green"
images ="http://placehold.it/330x508"

[[color.product]]
name = "Black"
cid = "black"
images = ""

In my product.html, I’m trying to get the colors listed with a delimiter ("|"). I read something about not being able to delimit range output, which I can confirm that I did not get working so I switched to this:

{{ $lc := .Site.Data.products.color.logo }}
{{ if isset  $lc .cid }}
  {{ delimit (index $lc .cid) "|" }}
{{ end }}

That keeps returning a blank.

Any ideas what I’m missing, or how I can get this to work?
Also, is it possible to have/manipulate an array as one of the key, value pairs in the map? That’s my next thing to tackle

example:

[color]
[[color.logo]]
images = ["image1","image2"]

Check your usage of isset and index. Second parameter should be a string of the field name.

{{ if isset $lc "cid" }}
{{   delimit (index $lc "cid") "|" }}

Thank you, but that still isn’t yielding anything.

using range gives me a list, but in the wrong format

{{ range $index, $lc := .color.logo }}
   {{ $options := index $lc "cid" }}
   {{- $options -}}
 {{- end -}}

renders:

          blue
          green
          black   

I need: blue|green|black

True. The problem is that delimit wants all the values at once, and you’re iterating over them one at a time.

Untested, but see if you can make this work:

{{ range $index, $lc := .color.logo }}
{{   $options := index $lc "cid" }}
{{   $.Scratch.SetInMap "colors" $options $options }}
{{ end }}
{{ delimit $.Scratch.GetSortedMapValues "colors" }}

For some weird and unknown reason I wasn’t able to get the Scratch method to work anywhere in the partial for any reason. I even tried just using the scratch examples from the docs without directly referencing the info from the data files like:

{{   $.Scratch.SetInMap "colors" "c1" "blue" }}

Any ideas on what I was doing wrong with that? I think delimit wasn’t working because the docs advise that it works on lists, groups, terms, and taxonomies - I didn’t initially take that to mean only, but here we are.

I did get @bep’s suggestion working:

{{ range $index, $lc := .color.logo }}
  {{ $option := index $lc "name" }}
  {{ if $index }}| 
  {{ end }}
  {{ $option }}
{{ end }} <!-- end color logo range -->

Thanks for the help guys!