If..Else based on frontmatter possible?


I’ve seen examples of conditional statements based on Site.Params i.e. based on things written in config.toml

Is it possible to make a conditional based on each .md file frontmatter too? I’m trying to make an option enableComment : true, which, if not present should not display internal disqus comments.

This is my partials/custom-content-footer.html

 <footer class=" footline" >
	{{with .Params.LastModifierDisplayName}}
	    <i class='fa fa-user'></i> <a href="mailto:{{ $.Params.LastModifierEmail }}">{{ . }}</a> {{with $.Date}} <i class='fa fa-calendar'></i> {{ .Format "02/01/2006" }}{{end}}
	{{ partial "custom-comments.html" . }}

And this is partials/custom-comments.html

{{ template "_internal/disqus.html" . }}

What am I supposed to do?

Disqus comments on select pages

You can wrap your Discus partial inside {{ with .Params.enableComment }}


An alternative is {{ if isset ...}}. I don’t recall the exact scenario, but I had an issue where I had false, but I think with counted that as set…

Go ahead and ignore this @siddhantrimal, unless with doesn’t work. :slight_smile:


Point is:

Go Templates treat the following values as false:
any zero-length array, slice, map, or string


1) isset: {{ if isset .Params "foobar" }}true:{{.Params.fooBar}}{{ else }}false:{{.Params.fooBar}}{{ end }}<br/>
2) if:    {{ if .Params.fooBar }}true:{{.Params.fooBar}}{{ else }}false:{{.Params.fooBar}}{{ end }}<br/>
3) with:  {{ with .Params.fooBar }}true:{{.}}{{ end }}<br/>

(the “foobar” is not a typo - see: https://gohugo.io/functions/isset)





fooBar: ""

you get:

1) isset: true:
2) if: false:
3) with: 



fooBar: 0

leads to:

1) isset: true:0
2) if: false:0
3) with: 


fooBar: false
1) isset: true:false
2) if: false:false
3) with: 

So actually it does matter…

It does not, as long as you have one of those:


1) isset: false:
2) if: false:
3) with: 


fooBar: true
1) isset: true:true
2) if: true:true
3) with: true:true


fooBar: 1
1) isset: true:1
2) if: true:1
3) with: true:1

So question is if “enable” is meant by giving just the parameter like so:


or actually setting a true value e.g.:

enableComment: true

In the first case it is more kind of a “flag”. The pure existence triggers an action but this may not be the common frontmatter style (?).

[SOLVED] display only 1 post from certain category with certain params “true”