HUGO

How to test for true/false values in front matter?

Hello,

I am quiet frustrated I cannot seem to get this to work. I am trying to add some content from my template depending on some front matter parameters…

That is. If I have front matter like so;

title: “Home”
draft: false
signature: “true”
comment: “true”

I want to test the signature parameter matter…below is my current attempt at code that fails, although I have tried many combinations of things

{{ if eq .Params.signature true }}
    {{ partial "signature" . }}
{{ end }}

If it’s of any use the following code does appear to work correctly to tell if the parameter has been set

 {{ if isset .Params "comment" }}
    {{ partial "comment" . }}
  {{ end }}

It’s a bit worrysome I cannot figure this out :expressionless: I been coding for 20 years lol…but any help would be really appreciated. Thank you…

The way you have set it in your front matter "true" is a string. So try wrapping it in quotes in the above if eq function.

But the best way to make a check in Hugo is the with function because if the variable is not present then the code block is escaped:

1 Like

YAML supports boolean values, so I suggest:

signature: true

And then:

{{ if .Params.signature }}
    {{ partial "signature" . }}
{{ end }}
5 Likes

Thanks guys, really helpful, appreciate it a lot.

It was setting is as a string that threw me off :slight_smile: I have got it working with the way bep suggested, the with thing didnt seem to behave, although I gave up on it pretty quick…will mess with it more tho…

I am not sure but I am new to HUGO, but I seem to find the documentation a bit cryptic. Not sure if that’s just me or not (I come from dotnet land and things are very different there).

Thank you kindly!
John

Because you are using Hugo early in its development, you are actually writing the documentation. So please, make it less cryptic. :slight_smile:

Point taken :slight_smile: I shall explore the link…It does require experience first before one can get in there and make worthwhile change though. I suppose I just wondered if it was me and increasing age or something :slight_smile: Thanks Maiki

How to check for false values?