Hugo not recognizing certain params in my front matter

Hello,

I’m on Hugo version 0.90.1 extended, and I’m seeing some odd behavior when firing up the dev server.

I have a single page template, recipes/single.html, connected to a content dir recipes.
content
├── about
│ ├── _index.md
│ └── about.md
├── blog
│ ├── _index.md
│ ├── post copy 2.md
│ ├── post copy.md
│ └── post.md
└── recipes
├── _index.md
├── dinner
│ ├── _index.md
│ └── citrus-marinated-chicken
│ ├── images
│ │ └── thumbnail.jpg
│ └── index.md
└── recipe.md

Now in my index.md in the citrus-marinated-chicken page bundle I’ve set some params.

---

title: "Citrus Marinated Chicken"

date: 2021-12-14T08:22:46-05:00

draft: false

summary:

  "A delicious, citrusy chicken recipe. Can be grilled or cooked inside. Can use the

  leftovers for an amzing chicken salad."

pre:

  prep: "30 mins"

  cook: "20 mins"

  serves: "3 to 5"

  difficulty: "Easy to make"

categories:

  - Dinner

  - Grill

tags:

  - Healthy

  - Chicken

  - Citrus

  - Grill

ingredients:

  - 2 pounds of chicken breast

  - 1/2 tablespoon of salt

  - 1 tablespoon olive oil

  - juice of 2 oranges and 2 lemons

  - 1 tablespoon smoked paprika

  - 1/2 tablespoon chipotle chili powder

  - 1 teaspoon black pepper

  - 1 teaspoon garlic powder

  - 1 teaspoon onion powder

stars: 4
---

And in my layouts/recipes/single.html template I try and read in .Params.stars

{{ with .Params.stars }}
    {{ $stars := . }}
    {{ $remainingStars := sub 5 $stars }}
{{ else }}
    {{ $stars := 0 }}
    {{ $remainingStars := sub 5 $stars }}
{{ end }}

And when I call the variable down in my code I get an error when firing up the server, that $stars does not exist

        <div>
            {{/*  Stars  */}}
            {{ range (seq 1 $stars) }}
                {{ partial "icons/star.html" (dict "color" "text-rose-800") }}
            {{ end }}
            {{/*  Remaining stars  */}}
            {{ if gt $remainingStars 0 }}
                {{ range (seq 1 $remainingStars) }}
                    {{ partial "icons/star.html" (dict "color" "text-slate-400") }}
                {{ end }}
            {{ end }}
        </div>

error message

I’ve tried creating the variable outside of the with statement but I still get the same error.

But when I comment out any reference to $stars and start the server, it starts up fine, and I then uncomment out the $stars code it works fine. Almost like the page is being rendered before it reads in the front matter.

I tried –disableFastRender and that did not help.

Any help will be greatly appreciated.

https://discourse.gohugo.io/t/how-to-check-for-nil-resource/35923/8

Thank you!

That worked.

I’d like to understand what I did wrong though. Why is assigning directly to the params value work for some but not all?

If you initialize ( := ) within a block, the variable is scoped (limited) to the block. This applies to if, with, and range.

https://pkg.go.dev/text/template#hdr-Variables

1 Like

But when i tried just doing the following outside of a block

{{ $stars := .Params.stars }}
{{ $remainingStars := sub 5 $stars }}

I would get an error

Start building sites … 
hugo v0.90.1-48907889+extended linux/amd64 BuildDate=2021-12-10T10:56:41Z VendorInfo=gohugoio
.File.Dir on zero object. Wrap it in if or with: {{ with .File }}{{ .Dir }}{{ end }}
Error: Error building site: failed to render pages: render of "page" failed: "/home/bab14/web_dev/hugo/mimijbakery.com/layouts/recipes/single.html:15:27": execute of template failed: template: recipes/single.html:15:27: executing "main" at <sub 5 .Params.stars>: error calling sub: can't apply the operator to the values

You are more likely to receive a prompt and accurate response if you post a link to the public repository for your project.

See https://discourse.gohugo.io/t/requesting-help/9132.

Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

I also get the following error in the same template

<h5 class="text-xs">{{ printf "%s/%s" (index .Params.categories 0) (index .Params.categories 1) }} Recipe</h5>
ERROR 2021/12/15 11:01:58 Failed to render pages: render of "page" failed: "/home/bab14/web_dev/hugo/mimijbakery.com/layouts/recipes/single.html:44:51": execute of template failed: template: recipes/single.html:44:51: executing "main" at <index .Params.categories 0>: error calling index: index of untyped nil
Total in 53 ms

.File.Dir

Your home page, taxonomy pages, and term pages are not backed by files, so you cannot use .File.Dir.
Compare to .Section or .Type instead.

I will look into the scope problem a little later.

I really appreciate the help. Thank you.

The other error is caused by this file:
https://github.com/bab014/mimijbakery/blob/master/content/recipes/recipe.md

It doesn’t have a stars value.

You can defend against this situation with:

{{ with .Params.stars }}
  ...
{{ end }}
1 Like

Ah, yeah that was a test file before I developed the structure I wanted for my single pages.

Seriously, thank you dude.

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