How to range mixed data

We all know how to range text or images - even data sets of those.
But how to range data sets that include both images and text lines? For instance if I want to range datasets like the following:

datasets:
  - itemname:
    category:
    price:
    image:
    image:
    image:
    image:

Thanks!

Your YAML has duplicate keys.

You mean a few images?
Yes, I need a few images. YAML, by specification, is not allowed to have duplicate keys in a mapping. So, how to overcome that restriction? If somebody knows.
Thanks!

Maybe try this.

image:
  - image 1
  - image 2
  - image 3

That creates nested data. I could not range nested data. Could you?

If I name images as
image-1
image-2
etc. to avoid duplicate data?

This (with a slight modification) might help

Dear @jmooring and @Arif , thank you very much! I’ve found the solution.

Share it here and mark it as the solution for others who might see this in future.

1 Like

Here is the solution I found:

The data structure:

imgs:
  - itemname: 
    itemdescription: 
    price: 
    images:
      - ""
      - ""
      - ""
  - itemname: 
    itemdescription: 
    price: 
    images:
      - ""
      - ""
      - ""

the HTML:

{{ if .Params.imgs }}
  {{ range .Params.imgs }}
    <div class="product-details">
      <h3>{{ .itemname }}</h3>
      <p>{{ .itemdescription }}</p>
      <p>Price: {{ .price }}</p>
    </div>
    <ul class="image-gallery">
      {{ range .images }}
        <li>
          <img src="{{ . | absURL }}" alt="">
        </li>
      {{ end }}
    </ul>
  {{ end }}
{{ end }}
1 Like

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