Hello Hugo users,
I am building my own blog with my own template using hugo. All of this has been a very steep learning curve for me, but I was able to resolve all challenges so far and I’m happy with the results and liking Hugo very much. I am almost done, but got stuck on the following.
I am trying to build a commenting system where I will have a YAML file for each comment, storing the comment files for each blog in their own directory. The relevant part of my project structure looks like this:
.
├── /content/blogs/article-a.md
├── /content/blogs/article-y.md
├── /content/blogs/article-z.md
├── /data/comments/article_a/
├── /data/comments/article_y/comment_a.yml
├── /data/comments/article_z/comment_a.yml
└── /data/comments/article_z/comment_b.yml
This is my environment:
C:\site\test1>hugo env
Hugo Static Site Generator v0.50 windows/amd64 BuildDate: 2018-10-29T09:53:09Z
GOOS="windows"
GOARCH="amd64"
GOVERSION="go1.11"
Right now, I have the following piece of code in the partial that builds the HTML body for my blog pages. I need to create every empty comment directory for every blog when using this. Doing so allows me to use readDir: if the comments folder for a blog is empty, there are no comments yet so I don’t have to display them on the page. I use the with conditional to check for this.
Then, I’m trying to put a variable into the range command. Each blog has a variable called page_id in the front matter. The contents of these variables correspond with the comment directory names for the blogs.
{{ $comment_prefix := "/data/comments/" }}
{{ $post_comment_path := (print $comment_prefix $.Params.page_id) }}
{{ $test_comment_path := readDir $post_comment_path }}
{{ with $test_comment_path }}
{{ $data_prefix := "$.Site.Data.comments." }}
{{ $blog_comment_path := (print $data_prefix $.Params.page_id) }}
{{ range $blog_comment_path }}
<b>Name: </b>{{ .name }}<br>
{{ end }}
{{ else }}
Be the first to leave a comment!
{{ end }}
Running this results in the following error:
C:\site\test1>hugo server -D
e[?25lBuilding sites … e[1;31mERRORe[0m 2019/03/06 17:32:19 [en] page "C:\\site\\test1\\content\\blogs\\20180917-using-docker-securely-on-management-servers.md": render of "page" failed: execute of template failed: template: blogs/single.html:5:3: executing "blogs/single.html" at <partial "body-conten...>: error calling partial: e[1;36m"C:\site\test1\themes\labtime\layouts\partials\body-content-blog.html:38:23"e[0m: execute of template failed: template: partials/body-content-blog.html:38:23: executing "partials/body-content-blog.html" at <$blog_comment_path>: range can't iterate over $.Site.Data.comments.y
e[KTotal in 196 ms
Error: Error building site: failed to render pages: [en] page "C:\\site\\test1\\content\\blogs\\20181103-announcing-some-lab-time-changes.md": render of "page" failed: execute of template failed: template: blogs/single.html:5:3: executing "blogs/single.html" at <partial "body-conten...>: error calling partial: e[1;36m"C:\site\test1\themes\labtime\layouts\partials\body-content-blog.html:38:23"e[0m: execute of template failed: template: partials/body-content-blog.html:38:23: executing "partials/body-content-blog.html" at <$blog_comment_path>: range can't iterate over $.Site.Data.comments.z
The stupid thing is that putting a static data reference into the range function with the same code works just fine, but obviously renders each page that has content in its comments directory with the same comments from the one blog I statically referenced:
{{ $comment_prefix := "/data/comments/" }}
{{ $post_comment_path := (print $comment_prefix $.Params.page_id) }}
{{ $test_comment_path := readDir $post_comment_path }}
{{ with $test_comment_path }}
{{ $data_prefix := "$.Site.Data.comments." }}
{{ $blog_comment_path := (print $data_prefix $.Params.page_id) }}
{{ range $.Site.Data.comments.z }}
<b>Name: </b>{{ .name }}<br>
{{ end }}
{{ else }}
Be the first to leave a comment!
{{ end }}
I don’t understand why the range function won’t accept a variable that holds the exact same string as I put into it with the static reference. To clarify this further, I get two errors saying this: range can’t iterate over $.Site.Data.comments.y|z
However, my working test literally contains code saying this: range $.Site.Data.comments.z. What’s the difference? Is there any way to resolve this behavior and achieve what I’m trying to do here?
I’ve got my project in this repo:
Only the comment code that I’m working on isn’t pushed to this repo yet, as the repo code is already live automatically at https://test.lab-time.it. I don’t want to put broken code into it.