Cannot do isset on .Keywords

I’m trying to create a meta-keywords in my template. I was thinking that if there are no keywords in the front matter of the page, it will fallback on the keywords parameter defined in config.toml.

This is what I have:

<meta name="keywords" content="{{if isset .Keywords}}{{.Keywords}}{{else}}{{.Site.Params.keywords}}{{end}}" />

However {{if isset .Keywords}} is breaking my template generating only

<meta name="keywords" content="

And then stops.

What is the correct way to check whether .Keywords has been set ot not

isset needs two parameters, so something like {{ if isset . "keywords" }}{{ .keywords }}.

I’ve tried that:

<meta name="keywords" content="{{if isset . "keywords"}}{{.Keywords}}{{else}}{{.Site.Params.keywords}}{{end}}" />

But the expression is not true, even though .Keywords is available. So .Site.Params.keywords is still being rendered.

Have you tried

{{ with .Keywords }}{{ . }}{{ else }}{{ .Site.Params.keywords }}{{ end }}
1 Like

I just did, and that breaks my entire partial template. Nothing in that partial is being rendered.

If “Keywords” is in the front matter and isn’t one of the blessed words, you have to access it

{{ if isset .Params "keywords" }} (Note the capitalization, too.)

Page Params

Any other value defined in the front matter, including indexes will be made available under .Params. 

There is something odd going on here. I can’t get it to work either with or without the Params prefix. Keywords is one of the blessed words so the .Params prefex shouldn’t be necessary.

If I create a value called metakeys and then use the code I gave with .Params.metakeys everything works perfectly. If I use any of the blessed words either than Keywords it also works perfectly.

I assume Keywords is a string?

The with syntax worked with description.

<meta name="description" content="{{with .Description}}{{.}}{{else}}{{.Site.Params.description}}{{end}}" />

The front matter looks as follows:

    ---
draft : false
title : "CV"
description : "Erfarenheter och meriter för Joachim Wallsin. Här listas utbildningar, arbetslivserfarenheter och övriga meriter."
keywords : "kungsbacka, göteborg, golfklubb"
date : 2014-12-10T01:33:41Z
menu:
    main:
        weight: 10
---

Just rendering {{.Keywords}} gives an ampty []. So maybe I’m using keywords wrong. It shouldn’t be a string, but an array?

I want to replace these accessors and methods with a pair of calls.

.Get "key" 

and

.Isset "key"

which ignore the difference between “blessed” and params and just work. For now we are stuck with operating in the go template space.

Correct. Keywords is expected to be an array of strings.

Ok, I think I got it now. Created a new param called metakeys and can access it by .Params.metakeys.

Just for the sake of it, what is the intended use of .Keywords?

<meta name="keywords" content="{{ with .Keywords }}{{ range . }}{{ . }}, {{ end }}{{ else }}{{.Site.Params.keywords}}{{ end }}">

html meta keywords & meta description

There’s a nice PR in place that would make this like:

<meta name="keywords" content="{{ with .Keywords }}{{ delimit . ", " }}{{ else }}{{ .Site.Params.keywords }}{{ end }}">
2 Likes