Using `if`, `else if`, `else` logic with frontmatter values

I have search the Tubes, and the community here, but haven’t found what I am looking for. I tried several things, but they do not work. What I am trying to do is get values from the frontmatter, and do things depending of their values.

For example, using his simple frontmatter:

--- 
title: “Test”
type: audio
--- 

I would like to use a logic on my template along these lines:

{{ if .Params.type "audio" }}
<span>Audio</span>
{{ else if .Params.type "url" }}
<span>URL</span>
{{ else if .Params.type "video" }}
<span>Video</span>
{{ else }}
<span>Note</span>
{{ end }}

That, of course, doesn’t work. :frowning_face:

Try something like this:

{{ if eq .Params.type "audio" }}
<span>Audio</span>
{{ else if eq .Params.type "url" }}
<span>URL</span>
{{ else if eq .Params.type "video" }}
<span>Video</span>
{{ else }}
<span>Note</span>
{{ end }}
3 Likes

That worked perfectly! Thank you very much. TIL!

2 Likes

my 2c

I recommend NOT using the parameter name “type”,
Every page has an attribute / value with the name “.Type”
This can lead to error with incorrect templates / spelling errors etc.
Better to use Params.mediatype.

2 Likes

Thank you, will change it.

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