Ben Bozzay
Ben Bozzay

Reputation: 121

Hugo Nested loop to iterate through multi-level YAML

I have page front matter with multiple levels. My page front matter looks like this:

grids:
- template: section
  background_image: "/uploads/2018/05/01/q-mark.png"
  rows:
  - template: row
    stack-columns: tablet-s
    cols:
    - template: column
      title: "Column-1 Title"
- template: section
  background_image: "/uploads/2018/05/01/lk.png"
  rows:
  - template: row
    stack-columns: tablet-s
    cols:
    - template: column
      title: "Column-2 Title"
    rows:
  - template: row
    stack-columns: tablet-l
    cols:
    - template: column
      title: "Column-3 Title"

I can display the first-level template name for both of my grids:

{{ range .Params.grids }}
   {{ .template }}
{{ end }}

How can I return the template name of rows and cols within the loop?

Upvotes: 6

Views: 2925

Answers (1)

Ben Bozzay
Ben Bozzay

Reputation: 121

I figured it out. You can use a nested range to access parameters with multiple levels:

    {{ range .Params.grids }}

      <p>Grid template name: {{ .template }}</p>

        {{ range .rows }}

          <p><strong>row template name: </strong>{{ .template }}</p>

          {{ range .cols }}

            <p><em>col template name:</em> {{ .template }}</p>

          {{ end }}

        {{ end }}

    {{ end }}

This displays:

Grid template name: section

row template name: row

col template name: column

Grid template name: section

row template name: row

col template name: column

Upvotes: 6

Related Questions