Range blog posts where param is equal to current page title

Hello community! I have HUGO + Netlify CMS project and I really don’t know why this code does not work, as it is simply the same as in doc examples. So I need some clue on where could be the problem? It does not show an error, but literally does nothing, but if I change where operator from “=” to “!=” it lists all articles.

Long story short, here is the code:

{{ $productTitle := .Params.title }} /* Current page title i.e: New Item Description 1*/
{{ range where (where .Site.RegularPages "Section" "blog" ) ".Params.relProd" "=" $productTitle }}
	<article class="article">
		{{ .Params.title }}
	</article>
{{ end }}

Here is example of article.md file:

---
title: Lorem ipsum dolor sit amet, consectetur
date: 2021-04-09T13:17:25.161Z
featuredUrl: /uploads/featuredImage.jpg
relProd: New Item Description 1
---
Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Here is part of config.yml file:

  - name: "blog"
	label: "Blog"
	folder: "content/blog"
	create: true
	fields:
	  - {label: "Title", name: "title", widget: string }
	  - {label: "Publish Date", name: "date", widget: datetime }
	  - {label: "Featured Image", name: "featuredUrl", widget: image }
	  - {label: "Related Product", name: "relProd", widget: relation,
		 collection: "products", search_fields: ["title"],
		 value_field: "title", required: false }
	  - {label: "Body", name: "body", widget: markdown }

Here is my repo: GitHub - albafox/aurora-de

themes/aurora/layouts/products/single.html

Change:

{{ range where (where .Site.RegularPages "Section" "blog" ) "Params.relProd" "=" $productTitle }}

To:

{{ range where (where .Site.RegularPages "Section" "blog" ) "Params.relprod" "=" $productTitle }}

.Params keys are converted to lower case internally. Most of the time you can reference your parameters without regard to case, but it will bite you with where, sort, and isset.

As a best practice, use snake_case when naming your site and page parameters.

Also, in your code, you can use .Title instead of .Params.title.

1 Like

Thanks for pointing out the issue, now it works perfectly, but still it’s a bit weird feeling…why Hugo converts case in some cases? Btw thanks for advice regarding snake_case, I usually do like that, but Hugo docs confused me a bit, as they use camelCase everywhere in code examples, so I decided to stick to best Hugo practises…my mistake)))

Yes, case handling of parameter keys is… different.

If you define timeWasted = true in a page’s front matter, it’s OK to do this:

{{ if .Params.timeWasted }}
  ...
{{ end }}

But not this:

{{ range where .Site.RegularPages ".Params.timeWasted" true }}
  ...
{{ end }}

This design decision was made about five years ago, and it still trips me up occasionally. You are not alone.

1 Like

Thanks for clarifying! :slight_smile:

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