Processing images in a data file

With the data structure below, how do I get the images to show up in the assets folder? `Do I need to move them to static and mount to assets?

[
  {
      "id": 1,
      "name": "Window Pane",
      "description": "The best window pane in town",
      "price": 99,
      "images": [
          "/images/SKUWP-window-pane.jpg",
          "/images/SKUWP2-window-pane.jpg",
          "/images/SKUWP3-window-pane.jpg"
      ],
      "category": "Windows"
  },
]

This code fails to show any images in the assets folder

Summary
{{ $products := .Site.Data.products }}
{{ range $index, $product := $products }}
  <h2>{{ $product.name }}</h2>
  
  <!-- Featured Image -->
  {{ $featuredImage := (index $product.images 0) }}
  {{ if $featuredImage }}
    {{ $featuredResource := resources.Get $featuredImage }}
    {{ if $featuredResource }}
      <div class="featured-image">
        <img src="{{ $featuredResource.RelPermalink }}" alt="{{ $product.name }} featured image">
      </div>
    {{ else }}
      <p>Featured image not found: {{ $featuredImage }}</p>
    {{ end }}
  {{ end }}

  <!-- Other Images -->
  <div class="product-images">
    {{ range $image := $product.images }}
      {{ $imageResource := resources.Get $image }}
      {{ if $imageResource }}
        <img src="{{ $imageResource.RelPermalink }}" alt="{{ $product.name }} image {{ $index }}">
      {{ else }}
        <p>Image not found: {{ $image }}</p>
      {{ end }}
    {{ end }}
  </div>
{{ end }}

A link to your repository might get you quicker answers than posting arbitrary code snippets without context.

Works now. Might have been one of those Hugo build quirks

just two hints

array index

  • arrays start at index 0
  • your featured image is defined as having index 0

so far so good

  • the rest of the images start with index 1
  • but the names start with 2

I would start them with 1 to have the same nubmer and index - will save some quirks later. maybe also name the featured one with a 0

id and name

there’s no relation between id and the SKUWP shown in the config … but that might be due to a stripped down example

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