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}}
	    </div>
	{{end}}
	{{ partial "custom-comments.html" . }}
</footer>

And this is partials/custom-comments.html

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

What am I supposed to do?

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:
false
0
any zero-length array, slice, map, or string

e.g.:

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: collections.IsSet | Hugo)

Given:

A)

---
fooBar:
---

or

---
fooBar: ""
---

you get:

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

Given

B)

---
fooBar: 0
---

leads to:

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

C)

---
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:

D)

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

E)

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

F)

---
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:

---
enableComment:
---

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 (?).

3 Likes