Use conditionals to load different head – if possible –

What I would like to do is use a conditional statement to load 1 of 2 head in Partials depending on the file loaded.

In single.html:

{{ if eq .Title "Frequently Asked Questions" }}
    {{ partial "head.html" . }}
{{ else }}
    {{ partial "head2.html" . }}
{{ end }}

So if it’s the frequently-asked-questions.md in Contents, then load head.html, else just load the other for all others.

Not sure if this is possible. I’m guessing my code is wrong but I’m trying to get there.

There are a lot of ways you could do this, but it depends on your use case really.

I have frequently based my heading based on whether there was a param set in my front matter.

So, let’s say I set heading: head Then:

{{ if $.Param "heading" }}
   {{ partial .Params.heading . }}
{{ else }}
   {{ partial "head2" . }}
{{ end }}

In this example, head2.html is the default and you can specify any valid heading in the front matter.

Thanks for the quick reply.

Alright, so in my case I want to only load head2 for a page titled: “Thank You”. But head1 for all others. So:

{{ if $.Params.title "Thank You" }}
   {{ partial "head2.html" . }}
{{ else }}
   {{ partial "head1.html" . }}
{{ end }}

Nevermind, I get an error for this. I believe my error is in defining for which page I want head2 to load. I thought I could do it with the pages’ title, but not happening.

You still need the eq function in your case, but don’t forget if you do not set title in the front matter of page the value will be <nil>, so the head1.html will render.

{{ if eq $.Params.title "Thank You" }}
  {{ partial "head2.html" . }}
{{ else }}
  {{ partial "head1.html" . }}
{{ end }}
1 Like

This work perfectly. Thanks a bunch!

1 Like