HTML header based on content type

I want to integrate the Open Graph protocol to my blog. This means that I will require to output different meta tag base on the content type.

For example, if I’m building a post, I’ll have the og:title set to the blog post’s title. But if I’m on the index page, I’ll want to set it to my blog’s name. I thought I could use something like that :

{{ if eq .Type "post" }}
   <meta property="og:title" content="{{ .Title }}" />
{{ else }}
   <meta property="og:title" content="{{ .Site.Title }}" />
{{ end }}

Would that be possible ? What would be the best approach to do this ?

Thanks for the help!

Oh and by the way, awesome tool ! I like it a lot!

This will work, but this is more general:

{{ if .IsPage }}

Se the doc for more variables of the same flavor.

I tried this in my header template:

{{ if eq .Type "post" }}
                <meta property="og:title" content="{{ .Title }}" />
 {{ end }}

But got this error:
ERROR: 2015/10/01 template: theme/partials/header.html:27:10: executing “theme/partials/header.html” at <.Type>: Type is not a field of struct type *hugolib.Node in theme/partials/header.html

Does .IsPage is true for list of posts ?

No. And yes, .Type is only available on Page, hence the error. List pages have no Type.

If you want all pages except the home page:

{{ if not .IsHome }}

So if I have homepage, I’d check .IsHome

If I want to target only single post page, I’d do {{ Not .IsPage and not .IsHome }} ? Or .IsNode ?

For single post page only: IsPage.

1 Like

Oh… it’s that easy! It will catch all posts and pages, but faire enough.

Is there a way to catch all list, like pagination ? Does only check if not .IsPage will work?

I got it all working. Thanks @bep !