Display post in list if front matter parameter has either of two values

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.

The format for the logical functions is:

(or Var1 Var2)

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:

(or (eq .Params.king "Link") (eq .Params.king "Article")

Putting that back into the range:

{{ 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

I tried:

{{ range (where (or (eq .Params.kind "Link") (eq .Params.kind "Article")).GroupByDate "Monday, 2 January 2006" }}

And got this error:

…parse failed: template: index.html:14: unclosed left paren

I then tried closing the parentheses:

{{ range (where (or (eq .Params.kind "Link") (eq .Params.kind "Article"))).GroupByDate "Monday, 2 January 2006" }}

And got this error:

…execute of template failed: template: index.html:14:16: executing "index.html" at <where>: wrong number of args for where: want at least 2 got 1

I agree that Go syntax is Yoda-esque, but I think even Yoda is clearer :slight_smile: Coming from Python it drives me a bit nuts!

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"))

1 Like

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. :thinking:

:cocktail: :books:


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.

You might want a different front matter value, we’ve already got a kind: https://gohugo.io/templates/section-templates/#page-kinds, and that might be confusing for ya later.

Also, if you change it to type, the above solution still works! See https://gohugo.io/content-management/front-matter#predefined and look at type.

1 Like

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!

1 Like

I personally find it easier to split the logic up instead of smooshing it all into a one liner.

{{ $bar := where .Site.RegularPages "Params.foo" "bar"}}
{{ $baz := where .Site.RegularPages "Params.foo" "baz"}}

{{ $combined := union $bar $baz }}

{{ range $combined.GroupByDate "January 2 2006" }}
  ...
{{ end }}

union: https://gohugo.io/functions/union

BTW:

  1. Are you sure it’s .Site.Pages you want, and not actually `.Site.RegularPages? See the difference.
  2. I’m not sure how stable it is to call a frontmatter param kind, as .Kind is a Page Variable and has meaning in Hugo.
2 Likes

Yea, I was thinking this would be easier (and I’d be more sure of it as well). Guess it was a challenge

Thanks, @pointyfar!

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 }}

Cheers, everyone!

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