HUGO

Help me understand why my partial is not showing?

Hello,

I am using staticman to add comments to a website. Comments get pushed to /data/comments directory as yml files.

data/
`-- comments
    |-- comment-1640218313969.yml
    `-- comment-1640227930656.yml

Here is comment-1640218313969.yml

_id: ee656ca0-6384-11ec-b29c-9f0b89f5415e
name: testy
email: b642b4217b34b1e8d3bd915fc65c4452
comment: number one comment.
date: '2021-12-23T00:11:53.964Z'

I am trying to use a partial to show the submitted comments, as described here.

  {{ $comments := readDir "data/comments" }}
  {{ $.Scratch.Add "hasComments" 0 }}
  {{ $entryId := .UniqueID }}

  {{ range $comments }}
    {{ if eq .Name $entryId }}
      {{ $.Scratch.Add "hasComments" 1 }}
      {{ range $index, $comments := (index $.Site.Data.comments $entryId ) }}
<blockquote>
  <p>{{ .comment | markdownify }}</p>
  <cite>
    <strong>{{ .name }}</strong><br>{{ dateFormat "02/01/2006" .date }}  
</cite>
</blockquote>
      {{ end }}       
    {{ end }}
  {{ end }}

  {{ if eq ($.Scratch.Get "hasComments") 0 }}
<p>Hey, be the first who comment this article.</p>
{{ end }}

I am unable to display any comments that are stored in /data/comments.
Thank you for any help understanding what I need to make this work.

You could simplify greatly by making an array containing your your site.Data.comments, for now they are stored in a giant map. Also you don’t need scratch anymore. Try something like this:

 {{/* Creating a slice holding your comments */}}
  {{ $globalComments := slice }}
  {{ range site.Data.comments }}
    {{ $globalComments = $globalComments | append . }}
  {{ end }}

  {{/* Setting default to $pageComments */}}
  {{ $pageComments := false }}

  {{ with $globalComments }}
    {{/* Finding the comments which match the name */}}
    {{ with where . "name" "testy" }}
      {{/* Storing their number in $pageComments */}}
      {{ $pageComments = len . }}
      {{/* Ranging on your comments */}}
      {{ range . }}
        <blockquote>
          <p>{{ .comment | markdownify }}</p>
          <cite>
            <strong>{{ .name }}</strong><br>{{ dateFormat "02/01/2006" .date }}
          </cite>
        </blockquote>
      {{ end }}
    {{ end }}
  {{ end }}
  
  {{ if not $pageComments }}
    <p>Hey, be the first who comment this article.</p>
  {{ end }}

Thank you. Is there a good reason not to use scratch? Just to simplify things?