Indexing YAML data for a given parameter on a post

That post started as a support ticket but I managed to find what I was seeking so here it is :

I wanted to a have a static comments system for my blog, that’s a really small blog with nearly no traffic and even less comments. Using a third-party comments system is great (You should check HashOver, privacy approved ) I do not want to have Javascript on my blog. So that’s the beginning of my static-commenting journey.

  • First I wanted to link a YAML file in the data folder to the post with the same name :
    {{ with (index .Site.Data.post (substr $.File.LogicalName 0 -3)) }}
  • Then I wanted to show comments with several informations :
    • Name
    • Website
    • Date
    • Body text

The YAML file looks like this :

comments:
  - id: "#1"
    name: "First Name"
    date: "2017-02-08"
    body: "That's Markdown"
    website: "gohugo.io"

  - id: "#2"
    name: "Second Name"
    date: "2017-02-09"
    body: "That's **Markdown**"
    website: "gohugo.io"

Now the most important part : the partial file (still a WIP) :

{{ with (index .Site.Data.post (substr $.File.LogicalName 0 -3)) }}
    <div class="comments">
      <div class="comment">
        {{ range .comments }}
        <h5>{{ .id }}</h5>
        {{if .website}}<a href="{{ .website | urlize | lower }}">{{ .name }}</a>
        {{ end }}
        <p>{{ .date }}</p>
      {{ if .body }}<p>{{ .body | markdownify }}</p>{{ end }}
      <p><br /></p>
      {{ end }}
      </div>
    </div>
{{ end }}

Thanks to Novelist.xyz for his blog post about data files and hugo