I would like to know if this is possible before I go this route (which would be tedious to create 909 files!). I have some data in a google sheet that I have converted to JSON and the entries look as below
[
{
"timestamp": "7/8/2021",
"parent": "/develop-life-skills/",
"name": "Markus",
"comment": "Lorem ipsum dola sit amet.",
"isAuthor": false {{/* or true if it is site author */}}
}
]
Just like in the linked comment, I would like to know if it is possible to pull the data from the JSON file to show only on the parent URL and how to do it?
I have this partial to render Disqus exported comments under the respective page.
I think you are doing something similar.
Yes.
anon44707125:
and how to do it?
These lines from my partial should give you a good idea.
Thank you very much! I managed to get it working using your example through some trial and error with the code below.
{{ $page := . }}
{{ $filtered := site.Data.comments }}
<div class="comments-wrap">
<div class="inside-comments">
{{ range $filtered }}
{{ $thread_link := ( .parent ) | replaceRE "^https?://[^/]+" "" }}
{{ $author := .isAuthor }}
{{ if (eq $thread_link $page.RelPermalink) }}
<div class="comment">
{{ if $author}}
<span class="cite author">{{ .name }} <span class="time">{{ .timestamp }}</span></span>
{{ .comment }}
{{ else }}
<span class="cite">Comment by {{ .name }} <span class="time">{{ .timestamp }}</span></span>
<p>{{ .comment }}</p>
{{ end }}
</div>
{{ end }}
{{ end }}
</div>
</div>
Now, How do I add <h4>Comments</h4>
after <div class="inside-comments">
that also appears conditionally when the comments are present? Similar to this perhaps?
GhsvsDe:
After $filtered
is defined you can use {{ if $filtered }}...{{end}}
whereever you want.
I tried this before asking but it did not work this time. It still shows up where there is no data.
GhsvsDe:
BUT: To use it inside the loop {{ range $filtered }}...{{end}}
would be nonsense.
Doing this adds the text before every comment/JSON entry. Not really what I want and I agree with you.
I use it to add a class to the author’s comments to style it differently. (It was working somehow now it is not.)
Sorry. Not my day today. Forget my comments. $filtered
is nearly never empty because you request the whole JSON entries in first line.
Try something like
{{ $filtered := where site.Data.comments ".parent" .RelPermalink }}
Then you have just the relevant comments from the beginning and can remove some code later on.
If I understand correctly that your comments.json contains all comments of all pages. E.g.
[
{
"timestamp": "7/8/2021",
"parent": "/hugo/summary/",
"name": "Markus",
"comment": "Lorem ipsum dola sit amet.",
"isAuthor": false
},
{
"timestamp": "7/8/2021",
"parent": "/hugo/summary/",
"name": "Markus",
"comment": "Lorem ipsum dola sit amet.",
"isAuthor": false
},
{
"timestamp": "7/8/2021",
"parent": "/hugo/aliases/",
"name": "Fritz",
"comment": "Lorem ipsum dola sit amet.",
"isAuthor": false
}
]
That worked! Thanks. (Changed timestamp format to yyyy-mm-dd
to use this . )
Full code if anyone is interested.
{{ $page := . }}
{{ $filtered := where site.Data.comments ".article_url" .RelPermalink }}
<div class="comments-wrap">
<div class="inside-comments">
{{ if $filtered }}
<h4>Comments</h4>
{{ end }}
{{ range $filtered }}
{{ $thread_link := ( .article_url ) | replaceRE "^https?://[^/]+" "" }}
{{ $author := .isAuthor }}
{{ if (eq $thread_link $page.RelPermalink) }}
<div class="comment">
{{ if $author}}
<span class="cite author">{{ .name }} <span class="time">{{ .time }}</span></span>
<p>{{ .comment | markdownify }}</p>
{{ else }}
<span class="cite">{{ .name }} <span class="time">{{ .time }}</span></span>
<p>{{ .comment | markdownify }}</p>
{{ end }}
</div>
{{ end }}
{{ end }}
</div>
</div>
system
Closed
March 19, 2022, 9:03pm
13
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.