Load Data from JSON by param in post

Hello.

I moved my old blog posts to Hugo.
Now i want save comments for posts and render it by partial.

I put comments data to separate files <postId>.json and save it in data/comments
Also I add variable postId to each post file.

Now i try to get comments for post in partial:

{{- $entryId := .Params.postId }}
{{- $commentsData :=  $entryId | .Site.Data.newsComments | printf "%s"}}

and it`s not working.

I need help.

Please post a link to the public repository for your site.

index should work, does it not?

{{ $commentsData :=  index .Site.Data.newsComments $entryId }}

I’d recommend using with to make sur your index actually returned something before using it.

link to partial template https://github.com/upodcast/site/blob/feature/comments/layouts/partials/disqus.html

Unfortunately, its doesnt help

{{- with .Params.entryId -}}
  {{- $commentsData :=  index $.Site.Data.newsComments . -}}
  {{ println . }}
  <ul>
  {{- range $commentsData }}
    <li>{{ .author }}</li>
  {{- end }}
  </ul>
{{- end -}}

Also, make sure you are in the feature/comments branch. The master branch doesn’t have any data files.

Thank you very much!

You are welcome. @regis had an excellent suggestion:

I’d recommend using with to make sure your index actually returned something before using it.

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