How to set the description parameter of a page from a template


#1

I have authors metadata like so:

title: Jane Doe
twitter: janedoe

In the head partial I have

{{ $defaultDescription := .Summary }}
<meta property="description" content="{{ .Description | default $defaultDescription }}">
<meta property="og:description" content="{{ .Description | default $defaultDescription }}">

I’d like the description of author pages to be dependent on title without my having to write it in the metadata, i.e. I’d like the authors list.html template to make the description of each author page “About title on website”. Is it possible? If so how? My googling was not too successful and the similar topics here didn’t help either. Thanks!


#2

Hi,

You could try something like this:

{{ if eq .Type "authors" }}
<meta property="og:description" content="About {{ .Title  }}">
{{ else }}
<meta property="og:description" content="{{ .Description | default $defaultDescription }}">
{{ end }}

#3

Thank you! So you’d recommend editing the head partial instead of the authors list.html template, right?


#4

That’s what I would personally do, yes. However, if authors has its own layout (with its own <head> defined), you should be able to skip the if-block and just declare

<meta property="og:description" content="About {{ .Title  }}">

I am personally a big fan of using baseof templates, which then means I can share the head partial among the other layouts.


#5

Yes it’s the same head partial for every page. :slightly_smiling_face: However I didn’t know about Base Templates, so nice, thanks for sharing. :ok_hand:

I was hoping to tweak the description parameter from the authors list template because this way the head template could remain clean and general. But maybe it’s not possible. :thinking:


#6

If you want only a different description for Author pages (and you might, maybe not all the pages on the site get .Title as their description), then you figure the best way to structure your code to only show that.

Some folks create conditionals, so they only have a single head partial to edit for all changes. Some folks use baseof templates and load different head partials based on their section or kind. It is up to how you prefer your code. :slight_smile:


#7

Thanks both! Now I know exactly what the possibilities are. :grinning: