There is a trick I use to map complex or even simple data:
{{ $class := "" }}
{{ $mapping := `
Apple: red
Kiwi: green
Samsung: blue
` | transform.Unmarshal }}
{{ range .Keywords }}
{{ with index $mapping . }}
{{ $class = . }}
{{ end }}
{{ end }}
<div class="{{ $class }}">[...]</div>
transform.Unmarshal can parse any json/yaml/toml string. Also, using backtick, you can have a multiple line strings in Go Template. The above example uses yaml to define a map of “keywords”/“color”.
If it’s global to the site, easiest route is to add it as a data file or as a Site param.
# data/class_mapping.yaml
Apple: red
Kiwi: green
Samsung: blue
{{ $mapping := .Site.Data.class_mapping }}
Now I’m not sure about yaml maps living at the root of a data file, you should check or use json or toml.
If it’s different depending on the section, you could add to the section front matter as part of the cascade key. This will make every page of that section inherit the value and will ease up on your retrieving it.
# content/my-section/_index.md
title: My Section
cascade:
class_mapping:
Apple: red
Kiwi: green
Samsung: blue