I want to display posts with the parameters of “Article” or “Link” set in their front matter.
I am trying this, but it doesn’t work:
{{ range (where .Site.Pages "Params.kind" "Link" or "Article").GroupByDate "Monday, 2 January 2006" }}
<h2 class="dateline">{{ .Key }}</h2>
{{ range .Pages }}
{{ if eq .Params.kind "Article" }}
...article displayed...
{{ else if eq .Params.kind "Link" }}
...link displayed...
{{ else }}
...nothing displayed...
{{ end }}
{{ end }}
{{ end }}
The error I get:
...execute of template failed: template: index.html:13:55: executing "index.html" at <or>: wrong number of args for or: want at least 1 got 0
If I remove or “Article”, it works, but obviously only displays the posts marked as Links.
I think I am having trouble with Go syntax or maybe something to do with the template context. I have also tried this syntax, but cannot get it to work so far.
But I’m not sure you’re checking up the kind of each item in the .Site.Pages correctly either. To check that equality, it would follow the same pattern as well:
(eq .Params.king "Link")
So what you’re really trying to do is or two of those:
{{ range (where (or (eq .Params.king "Link") (eq .Params.king "Article")).GroupByDate "Monday, 2 January 2006" }}
I can’t easily test this but you should be able to see if that gets you closer, and the error messages are pretty accurate. For some reason I don’t see or and and in the function list, but here’s an example in the templates introduction
I’ll add as a note that Hugo’s (golang’s) function syntax still throws me for a loop sometimes. Very different than many other programming languages, very Yoda-esque
Yea, this is why it’s easier in front of you. I left out the .Site.Pages, which changes things…remembering how where function works. Try this:
{{ range where .Site.Pages .Params.kind (or "Link" "Article").GroupByDate "Monday, 2 January 2006" }}
Which is closer to what you had originally, just the or was out of order. Which is actually what I typed first, before I forgot where doesn’t need the equality function, it’s build in.
If that doesn’t work, I’d try the or earlier in the statement:
where .Site.Pages (or (.Params.kind "Link") (.Params.kind "Article"))
I’m not going to address the logic part (I’d have to build a repo myself, not into it), but I did want to mention https://gohugo.io/templates/views/. I think that is what you actually want. It would be something like:
{{ range .Site.Pages.GroupByDate "Monday, 2 January 2006" }}
<h2 class="dateline">{{ .Key }}</h2>
{{ range .Pages }}
{{ .Render "summary"}}
{{ end }}
{{ end }}
And then you make:
layouts/_default/summary.html
layouts/article/summary.html
layouts/link/summary.html
And then, I don’t know, cocktails and light reading, or whatever folks do when they get to not figure out conditional logic.
Oops, nope. I misunderstood how your data was structured, because you include an “else” for neither front matter param, while the first range would never return those, so… anyhow, the above works if you put links in their own section, etc.
Thank you @angus & @maiki. I appreciate your help very much.
I still cannot get it to work after trying both of @angus’ suggestions, but I appreciate the help, and also the pointer to the views page.
I suspect that something else may be causing an issue, so I am starting from scratch with the Hugo quick start template and theme and hope to be back with a solution, or perhaps more questions, if I may, soon.
To be clear I am an amateur working on converting a blog that is written in an old version of Python & Django to Go & Hugo, and am currently trying to get the homepage working. ¶ From the looks of things I will need to put on a wizard’s cap to get the linked list archive page working, too, and this is what I plan to try first.
When I am not figuring out conditional logic, I am usually drawing, writing, walking, cooking, eating, daydreaming or playing videogames!
Your suggestion to use .Site.RegularPages made things work independently of logic code, outputting only Links and Articles. So this works:
{{ range .Site.RegularPages.GroupByDate "Monday, 2 January 2006" }}
Splitting up the logic worked, too, and I changed the name of the kind parameter to ‘contentkind’ to make sure it does not conflict with the Page Variable you mentioned. Here is the working code using this method:
{{ $link := where .Site.RegularPages "Params.contentkind" "Link" }}
{{ $article := where .Site.RegularPages "Params.contentkind" "Article" }}
{{ $combined := union $link $article }}
{{ range $combined.GroupByDate "Monday, 2 January 2006" }}
<h2 class="dateline">{{ .Key }}</h2>
{{ range .Pages }}
{{ if eq .Params.contentkind "Article" }}
...article displayed...
{{ else if eq .Params.contentkind "Link" }}
...link displayed...
{{ else }}
...nothing displayed...
{{ end }}
{{ end }}
{{ end }}