How to assign map in front matter archetype?

In my site config file (config.yaml) I have this:

author:
  name: "Ben Souchet"
  email: "contact@bensouchet.dev"
  url: "https://bensouchet.dev"

In my custom theme posts.md archetype I want to copy the author map from the site config file, like this:

author: {{ .Site.Author }}

Issue

I cannot access the variable as a map in the posts.md layout like this:

{{ .Page.Params.author.name }}

because Page.Params.author is a string with this value:
map[email:contact@bensouchet.dev name:Ben Souchet url:https://bensouchet.dev]

So my question is, how to assign a map by copy in front matter ?

Without having a solution to your particular problem, why would you want to copy the config info into each post? One solution is to set the layout templates to look for this information in your frontmatters and if it exsits, use it, but if it doesn’t exist, use the config information. Then you don’t need it on each file.

Or if you do need it on each file, but you never use the params directly from the config in any layout templates, then just put that information in the archetype itself.

Or of course you may have your own reasons for doing what you seek to do, but those are some alternative ways to consider the problem.

I wanted to avoid using taxonomy to define authors, and I wanted to set a default author variables in each post.
So after users have created a new post via hugo new, they can edit the post front matter to set the right author info (replace default values).

To summarize It’s a way to set default front matter values for author (based on the author defined in the config file).

I imagine I can do (in the archetype file) something like:

author:
  name: {{ .Site.Author.name }}
  email: {{ .Site.Author.email }}
  url: {{ .Site.Author.url }}

But if in the future more elements are handled by the theme, like author.mood, I will need to edit the archetype to add:

author:
  ...
  mood: {{ .Site.Author.mood }}

to ensure a default value is added to new post.

So still interested to know if map assignation via copy is possible in front matter :slight_smile:

1 Like

You can use this:

---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
{{ dict "author" .Site.Author | transform.Remarshal "yaml" -}}
---

NOTE: To handle YAML indentation correctly you must provide the root key (the author) as part of the dict map.


Something like this, wont work, because produce wrong indentation

---
author: {{ .Site.Author | transform.Remarshal "yaml" -}}
---
2 Likes

It works beautifully, thank you very much!

1 Like

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