# YAML for datapage: how to organize?

**URL:** <https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898>\
**Category:** support\
**Created:** [August 9, 2022, 12:47am UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898 "2022-08-09T00:47:54Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rufus42](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rufus42/32/17482_2.png) [@rufus42](https://discourse.gohugo.io/u/rufus42)\
**Post date:** [August 9, 2022, 12:47am UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/1 "2022-08-09T00:47:54Z")

</div>

Hello everyone,

first of all thank you for this great software and helpful forum.

I have a file `data/testfile.yaml`:

```auto
- date: 1905
  title: some event
- date: 1905
  title: some other event
- date: 1910
  title: something always happens

```

I have a shortcode `themes/<theme name>/layouts/shortcodes/read-testfile.html`:

```auto
{{ range sort site.Data.history "date" }}
  <h2>{{ .date }}</h2>
  <h3>{{ .title | markdownify }}</h3>
{{ end }}

```

I get the expected output.  
I get an h2 heading for each `date` and an h3 heading for each `title`.

I would like the `date` heading to be output only once, even if there are multiple `titles`.  
So using the above example

```nohighlight
<h2>1905</h2>
<h3>some event</h3>
<h3>another event</h3>
<h2>1906</h2>
<h3>something always happens</h3>

```

- How do I need to build my `testfile.yaml`?
- How can I query it in my shortcode?

Full of hope for beginner friendly hints.

Many greetings

Original text in German, see below.  
Translated with [DeepL Translate: The world's most accurate translator](http://www.DeepL.com/Translator) (free version)

* * *

Hallo miteinander,

zunächst vielen Dank für diese großartige Software und dieses hilfreiche Forum.

Ich habe eine Datei `data/testdatei.yaml`:

```auto
- datum: 1905
  title: irgendein Ereignis
- datum: 1905
  title: ein anderes Ereignis
- datum: 1910
  title: irgendwas passiert immer

```

Ich habe einen Shortcode `themes/<themename>/layouts/shortcodes/read-testdatei.html`:

```auto
{{ range sort site.Data.geschichte "datum" }}
  <h2>{{ .datum }}</h2>
  <h3>{{ .title | markdownify }}</h3>
{{ end }}

```

Ich erhalte den erwarteten Output.  
Ich bekomme für jedes `datum` eine h2-Überschrift und für jeden `title` eine h3-Überschrift.

Ich möchte gerne, dass die `datum`-Überschrift nur einmal ausgegeben wird, auch wenn es mehrere `title` gibt.  
Also an obigem Beispiel

```nohighlight
<h2>1905</h2>
<h3>irgendein Ereignis</h3>
<h3>ein anderes Ereignis</h3>
<h2>1906</h2>
<h3>irgendwas passiert immer</h3>

```

- Wie muss ich meine `testdatei.yaml` aufbauen?
- Wie kann ich sie in meinem Shortcode abfragen?

Voller Hoffnung auf anfängerfreundliche Hinweise.

Viele Grüße

Originaltext in Deutsch, siehe unten

---

<div class="post-metadata">

**Author:** ![gaetawoo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gaetawoo/32/16789_2.png) [@gaetawoo](https://discourse.gohugo.io/u/gaetawoo)\
**Post date:** [August 9, 2022, 2:24am UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/2 "2022-08-09T02:24:58Z")

</div>

I’m sorry I’m on mobile so I can’t really help with code sample at the moment but I would think the best way would be to setup your data file

```auto
- date:
  - year: 1905
  - title:
    - something happened
    - something else
- date:
  - year: 1906
  - title:
    - nothing
- date:
Etc

```

Then you range through the dates, apply the year for header 2 and range through the titles applying header 3. If there is only one title then it only does one.

---

<div class="post-metadata">

**Author:** ![rufus42](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rufus42/32/17482_2.png) [@rufus42](https://discourse.gohugo.io/u/rufus42)\
**Post date:** [August 9, 2022, 12:27pm UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/3 "2022-08-09T12:27:41Z")

</div>

Hello gaetawoo,

thank you for your helpful hint regarding the structure of the YAML file.

With the shortcode

```auto
{{ range site.Data.testfile }}
{{ range .date }}
{{ if .year }}<h2>{{ .year }}</h2>{{ end }}
{{ if .title }}<h3>{{ .title }}</h3>{ end }}
{{ end }}
{{ end }}

```

I get the output

```auto
<h2>1905</h2>
<h3>[something happened something else]</h3>
<h2>1906</h2>
<h3>[nothing]</h3>

```

Now I want to have a h3 heading for each `.title`, not for the whole array(?).

The shortcode

```auto
{{ range site.Data.testfile }}
{{ range .date }}
{{ if .year }}<h2>{{ .year }}</h2>{ end }}
{{ range .year }}
{{ if .title }}<h3>{{ .title }}</h3>{ end }}
{{ end }}
{{ end }}
{{ end }}

```

leads to the error message

`"D:\DEVELOPMENT\hugo\sites\ra-master\content\showcase\datensaetze.md:49:1": failed to render shortcode "data-testfile": failed to process shortcode: "D:\DEVELOPMENT\hugo\sites\ra-master\themes\ra-master\layouts\shortcodes\data-testfile. html:11:9": execute of template failed: template: shortcodes/data-testfile.html:11:9: executing "shortcodes/data-testfile.html" at <.year>: range can't iterate over 1905`

What am I doing wrong?

Many greetings

* * *

Hallo gaetawoo,

vielen Dank für deinen hilfreichen Hinweis bzgl. des Aufbaus der YAML-Datei.

Mit dem Shortcode

```auto
{{ range site.Data.testdatei }}
{{ range .date }}
{{ if .year }}<h2>{{ .year }}</h2>{{ end }}
{{ if .title }}<h3>{{ .title }}</h3>{{ end }}
{{ end }}
{{ end }}

```

erhalte ich den Output

```auto
<h2>1905</h2>
<h3>[something happened something else]</h3>
<h2>1906</h2>
<h3>[nothing]</h3>

```

Nun möchte ich für jeden `.title` eine h3-Überschrift haben, nicht für das gesamte Array(?).

Der Shortcode

```auto
{{ range site.Data.testdatei }}
{{ range .date }}
{{ if .year }}<h2>{{ .year }}</h2>{{ end }}
{{ range .year }}
{{ if .title }}<h3>{{ .title }}</h3>{{ end }}
{{ end }}
{{ end }}
{{ end }}

```

führt zu der Fehlermeldung

`"D:\DEVELOPMENT\hugo\sites\ra-master\content\showcase\datensaetze.md:49:1": failed to render shortcode "data-testdatei": failed to process shortcode: "D:\DEVELOPMENT\hugo\sites\ra-master\themes\ra-master\layouts\shortcodes\data-testdatei.html:11:9": execute of template failed: template: shortcodes/data-testdatei.html:11:9: executing "shortcodes/data-testdatei.html" at <.year>: range can't iterate over 1905`

Was mache ich falsch?

Viele Grüße

---

<div class="post-metadata">

**Author:** ![gaetawoo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gaetawoo/32/16789_2.png) [@gaetawoo](https://discourse.gohugo.io/u/gaetawoo)\
**Post date:** [August 9, 2022, 1:08pm UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/4 "2022-08-09T13:08:09Z")

</div>

> [@rufus42](#):
>
> ```auto
> {{ range site.Data.testfile }}
> {{ range .date }}
> {{ if .year }}<h2>{{ .year }}</h2>{ end }}
> {{ range .year }}
> {{ if .title }}<h3>{{ .title }}</h3>{ end }}
> {{ end }}
> {{ end }}
> {{ end }}
> 
> ```

The reason you are getting your error is because you are trying to iterate over year which is not an array, and title isn’t part of year, it’s part of date. So something like this might work (i’m not able to test it):

```auto
{{ range site.Data.testfile }}
  {{ range .date }}
    {{ with .year }}<h2>{{ . }}</h2>{ end }}
    {{ range .title }}
      {{ with .title }}<h3>{{ . }}</h3>{ end }}
    {{ end }}
  {{ end }}
{{ end }}

```

Title is the array over which you iterate, not year. Both title and year are children of date, they are not otherwise directly related.

---

<div class="post-metadata">

**Author:** ![rufus42](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rufus42/32/17482_2.png) [@rufus42](https://discourse.gohugo.io/u/rufus42)\
**Post date:** [August 9, 2022, 2:09pm UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/5 "2022-08-09T14:09:59Z")

</div>

Hello gaetawoo,

thanks again for your generous support.

Your shortcode

```auto
{{ range site.Data.testfile }}
  {{ range .date }}
    {{ with .year }}<h2>{ . }}</h2>{ end }}
    {{ range .title }}
      {{ with .title }}<h3>{ . }}</h3>{ end }}
    {{ end }}
  {{ end }}
{{ end }}

```

leads to the following error message

`"D:\DEVELOPMENT\hugo\sites\ra-master\content\showcase\datensaetze.md:49:1": failed to render shortcode "data-testdatei": failed to process shortcode: "D:\DEVELOPMENT\hugo\sites\ra-master\themes\ra-master\layouts\shortcodes\data-testdatei.html:26:14": execute of template failed: template: shortcodes/data-testdatei.html:26:14: executing "shortcodes/data-testdatei.html" at <.title>: can't evaluate field title in type interface {}`

Too bad, found your code so clear and logical when reading it, I don’t understand the error message.

Do you have another idea for me?  
Either way, thank you and best regards

* * *

Hallo gaetawoo,

noch einmal vielen Dank für deine großzügige Unterstützung.

Dein Shortcode

```auto
{{ range site.Data.testdatei }}
  {{ range .date }}
    {{ with .year }}<h2>{{ . }}</h2>{{ end }}
    {{ range .title }}
      {{ with .title }}<h3>{{ . }}</h3>{{ end }}
    {{ end }}
  {{ end }}
{{ end }}

```

führt zu folgender Fehlermeldung

`"D:\DEVELOPMENT\hugo\sites\ra-master\content\showcase\datensaetze.md:49:1": failed to render shortcode "data-testdatei": failed to process shortcode: "D:\DEVELOPMENT\hugo\sites\ra-master\themes\ra-master\layouts\shortcodes\data-testdatei.html:26:14": execute of template failed: template: shortcodes/data-testdatei.html:26:14: executing "shortcodes/data-testdatei.html" at <.title>: can't evaluate field title in type interface {}`

Schade, fand deinen Code beim Lesen so klar und logisch, die Fehlermeldung verstehe ich nicht.

Hast du noch eine Idee für mich?  
So oder so, Danke und viele Grüße

---

<div class="post-metadata">

**Author:** ![gaetawoo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gaetawoo/32/16789_2.png) [@gaetawoo](https://discourse.gohugo.io/u/gaetawoo)\
**Post date:** [August 9, 2022, 2:37pm UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/6 "2022-08-09T14:37:04Z")

</div>

I’m sorry, i do see a syntax bug in what i posted before.  
The inner `with .title` is wrong in this context

Maybe the following.

```auto
{{ range site.Data.testfile }}
  {{ range .date }}
    {{ with .year }}<h2>{ . }}</h2>{ end }}
    {{ range $title := .title }}
      {{ with $title }}<h3>{ . }}</h3>{ end }}
    {{ end }}
  {{ end }}
{{ end }}

```

I’m saving the context of the inner range to a variable and using with with the variable. The previous way was using with with something that didn’t exist.

---

<div class="post-metadata">

**Author:** ![rufus42](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rufus42/32/17482_2.png) [@rufus42](https://discourse.gohugo.io/u/rufus42)\
**Post date:** [August 9, 2022, 4:39pm UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/7 "2022-08-09T16:39:08Z")

</div>

Hello gaetawoo,

your shortcode produces neither output nor error message. ☹

Thanks again and best regards

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [August 9, 2022, 5:06pm UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/8 "2022-08-09T17:06:21Z")

</div>

data/testfile.yaml

```nohighlight
- date: 1905
  title: some event
- date: 1905
  title: some other event
- date: 1910
  title: something always happens

```

layouts/shortcodes/read-testfile.html

```auto
{{ $dates := slice }}
{{ range site.Data.testfile }}
  {{ $dates = $dates | append .date }}
{{ end }}
{{ $dates = $dates | uniq | sort }}

{{ range $dates }}
  <h2>{{ . }}</h2>
  {{ range where site.Data.testfile "date" . }}
    <h3>{{ .title }}</h3>
  {{ end }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![gaetawoo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gaetawoo/32/16789_2.png) [@gaetawoo](https://discourse.gohugo.io/u/gaetawoo)\
**Post date:** [August 9, 2022, 5:27pm UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/9 "2022-08-09T17:27:56Z")

</div>

Yea, this is definitely the way to do it. Thanks @jmooring

---

<div class="post-metadata">

**Author:** ![gaetawoo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gaetawoo/32/16789_2.png) [@gaetawoo](https://discourse.gohugo.io/u/gaetawoo)\
**Post date:** [August 9, 2022, 8:23pm UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/10 "2022-08-09T20:23:46Z")

</div>

> [@rufus42](#):
>
> your shortcode produces neither output nor error message

I finally got a chance to test this out. I’m using a fresh, totally bare bones layout template.

My last suggestion works on my end. It produces the expected/desired html.

It’s not as pretty on the data file side as @jmooring nor as elegant on the templating side… but it does work.

_/data/testfile2.yaml_

```auto
- date:
  - year: 1905
  - title:
    - something happened
    - something else
- date:
  - year: 1906
  - title:
    - nothing

```

_template_

```auto
try3
{{ range site.Data.testfile2 }}
  {{ range .date }}
    {{ with .year }}<h2>{{ . }}</h2>{{ end }}
      {{ range .title }}
        {{ with . }}<h3>{{ . }}</h3>{{ end }}
      {{ end }}
  {{ end }}
{{ end }}

<br>

try4
{{ range site.Data.testfile2 }}
  {{ range .date }}
    {{ with .year }}<h2>{{ . }}</h2>{{ end }}
      {{ range $title := .title }}
        {{ with $title }}<h3>{{ . }}</h3>{{ end }}
      {{ end }}
  {{ end }}
{{ end }}

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/gohugo/original/3X/2/f/2fd30e1800cbec4aeabd7876f183d0c28b039877.png)

So while I would still use @jmooring solution, just for educational sake, it does work. I’m not sure why you would have seen no output and no error. And it’s valuable to put in some debugging `{{ print "test" }}` or something statements in the code to see what is getting processed and when. Or `{{ warnf "something" }}` to have it write to the output console.

Also, i’m not doing it in a shortcode, i’m doing it in the actual layout template so while that might be a difference, given the code and the scope, i don’t see how it could be.

EDIT: Below

Also the below works:

_testfile3.yaml_

```auto
- date: 1905
  title:
    - something happened
    - something else
- date: 1906
  title:
    - nothing

```

_template_

```auto
try6
{{ range $item := site.Data.testfile3 }}
  {{ with .date }}<h2>{{ . }}</h2>{{ end }}
      {{ range $item.title }}
        {{ with . }}<h3>{{ . }}</h3>{{ end }}
      {{ end }}
{{ end }}

```

The outputs are all the same as pictured above.

---

<div class="post-metadata">

**Author:** ![rufus42](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rufus42/32/17482_2.png) [@rufus42](https://discourse.gohugo.io/u/rufus42)\
**Post date:** [August 10, 2022, 8:37am UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/11 "2022-08-10T08:37:30Z")

</div>

Hello **jmooring** ,

thanks a lot for your code.  
Could try it last night, it works.  
Your code helped me a lot.

Please contradict me if I misunderstand the “slice part” of your code.

`{ $dates := slice }}`: `slice` creates a new array `$dates`.  
`{ $dates = $dates | append .date }}`: Append to the array `$dates` the value of the current record `.date`.  
`{ $dates = $dates | uniq | sort }}`: array is sorted and duplicate values are removed

Many greetings

---

<div class="post-metadata">

**Author:** ![rufus42](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rufus42/32/17482_2.png) [@rufus42](https://discourse.gohugo.io/u/rufus42)\
**Post date:** [August 10, 2022, 8:38am UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/12 "2022-08-10T08:38:36Z")

</div>

Hello **gaetawoo** ,

thanks a lot for checking your code.  
No idea why that one didn’t work for me.  
Will probably not have time to deal with it until the weekend unfortunately.  
I especially like `try6` because of the clear structure of the underlying `testfile3.yaml`.

Thank you very much for your support/help and especially for the explanatory words about the code.

Many greetings

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [August 12, 2022, 8:39am UTC](https://discourse.gohugo.io/t/yaml-for-datapage-how-to-organize/39898/13 "2022-08-12T08:39:06Z")

</div>

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