Access data in front matter

On the page it says “This feature can extend the content in case your front matter fields grow out of control.” This is exactly what happened in my case. I’m sorry but there is no explanation in the docs how to implement this.

Let’s say I have the following two pages…

title: "page1"
description: "description1"
url: "page1"

bla bla

title: "page2"
description: "description2"
url: "page2"

bla bla

…and I want to fetch the two front matter variables title and description from the following json file:


   "url": "page1",
   "title": "title1",
   "description": "description1"
   "url": "page2",
   "title": "title2",
   "description": "description2"

How can I achieve this?

You cannot override built-in page variables.

This feature can extend the content in case your front matter fields grow out of control.

I think the important word is extend. Meaning, if you need 10 additional fields on each page of a particular content type, it might be easier to store those fields in a data file.

I don’t want to overwrite the built-in variables.

I want something like

title: .Site.Data.metadata.title
description: .Site.Data.metadata.description
url: .Site.Data.metadata.url

bla bla

instead of

title: "page1"
description: "description1"
url: "page1"

bla bla

Background information

I have a website with 1000+ pages and I want to outsource the title and the description for easier maintaining.

Sorry, poor choice of words.

Front matter variables must be set to literal values: strings, integers, floats, booleans, datetime, etc. You cannot assign a front matter variable using a function, method, another variable, etc.

1 Like

I want to do exactly the same thing!
Is there some workaround? It would be extremely handy to be able to set front matter variables programmatically.

My initial use case is to look up the page title from a database (as the requester has it), but I could see other uses (like looking up a translation for a title).

something like (yes, I know this doesn’t work):
title: {{ (index .Site.Data.weeks (string "2")).name }}
title: {{< weekname 2 >}}

where weekname is a shortcode

That said: I can see why this is hard to support in general. The thing which would be awesome (allow shortcodes in the frontmatter) would require hacking the metafile parserts. Plus, it would have all kinds of semantic ambiguities (how can you process the metadata without access to the metadata).

So, I was hoping someone had a clever way to achieve the effect easily. The best I can think of is a pre-processing script that sets the titles accordingly.

You could consider processing this layout-side instead of content-side.

<!-- md: -->
title: "{{ something foo }}"

<!-- template -->
{{ $title := .Title }}
{{ if strings.HasPrefix $title "{{" }} <!-- check if $title has '{{}}' -->
    <!-- remove {{}} and split at spaces -->
    {{ $x := split (strings.TrimPrefix "{{" (strings.TrimSuffix "}}" $title )) " "}} 
    {{ if eq (index $x 0) "something"}} <!-- process accordingly -->
        <!-- get processed value; pass $x so you have access to param "foo" -->
        {{ $title = partial "something" (dict "context" . "params" $x) }}
    {{ end }}
{{ end }}
<a href="{{.RelPermalink}}">{{$title}}</a>

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