if statments in golang hugo

Viewed 231

I'm trying to create HTML layout partial to add different html elements using the front matter as a data source and hugo templating for the rendering. For example the front matter has this property

---
animal:
  type:"bear"
---

now the partial looks like this:

{{ if  eq .Params "animal.type" "dog" | or eq .Params "animal.type" "bear" | or eq .Params "animal.type" "fox" | or eq .Params "animal.type" "wolf" }}

<p> Belongs to the Canine family, they have distinctive fangs. <p>

{{ end }}

throws the following error when i run hugo server:

executing "partials/canine.html" at : wrong number of args for eq: want at least 1 got 0

Am i doing the piping of the or operators wrong ? Or what is the issue ?

the folder structure looks like this: the single.html is calling the animalsDetails.html this one is calling canine.html I don't think the it is the problem, but just in case.

|layouts 
  |_defaults
    single.html
  |partials
    animalDetails.html
    canine.html
2 Answers

You probably need to use .Params properly and ensure use of round braces.

Try this (not tested):

{{ if  eq .Params.animal.type "dog" | or (eq .Params.animal.type "bear") | or (eq .Params.animal.type "fox") | or (eq .Params.animal.type "wolf") }}

<p> Belongs to the Canine family, they have distinctive fangs. <p>

{{ end }}

I would suggest against using bars in Hugo expressions, I have ran into problems with them (though that was a case of replaceRE).

Here's the solution w/o use of bars:

{{ if or (eq .Params.animal.type "dog") (eq .Params.animal.type "bear") (eq .Params.animal.type "fox") (eq .Params.animal.type "wolf") }}

<p> Belongs to the Canine family, they have distinctive fangs. <p>

{{ end }}

Feel absolutely free to comment if you need more help!

Edit 1: Fixed a typo and added alternative recommended solution.

{{ if or (eq .Params.animal.type "dog") (eq .Params.animal.type "bear") (eq .Params.animal.type "fox") (eq .Params.animal.type "wolf") }}

<p> Belongs to the Canine family, they have distinctive fangs. <p>

{{ end }}

that did it

Related