How to use a block multiple times?

Hi there,

So I have a block in my layout for the title.

<title>{{ block "title" . }}{{ .Site.Params.Company }} - {{ .Title }}{{end}}</title>

So that I can have a default title and override it in some pages, now I need to have the same title for the og:title meta tag, but I can’t seem to find a way to reuse it, if I just add:

<meta name="og:title" content="{{ block "title" . }}{{ .Site.Params.Company }} - {{ .Title }}{{end}}">

I errors saying I can’t define it twice, I was trying to save it in a variable to use it but also no success, what would be the proper way to achieve this?


I’d handle it with page-scoped scratch variables.

For example, here are the first three lines of baseof.html:

<!doctype html>
{{ partial "functions/set-page-scoped-scratch-vars.html" . }}


{{- $pageTitle := printf "%s - %s" .Site.Params.Company .Title -}}
{{- .Scratch.Set "pageTitle" $pageTitle -}}

Then, in templates, partials, or shortcodes you can get this value with:

{{ .Scratch.Get "pageTitle" }}

This approach is useful for things like body classes, page descriptions, etc.

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