Ability to access .Site.Params in the front matter of archetypes


#1

I’ve been working on a theme and would like to include gists as an archetype. In the front matter, there is a github_user and a gist_id which need to be filled in. It would be a nice time-saver to automatically populate the user field from the site config params, if the user wants a default author that isn’t defined directly in the archetype itself (for better reuse). I’ve seen a few issues on github and here with similar problems, but nobody proposing them directly in the archetypes themselves.

The use case would be the quality of life and reuse provided by the flexibility of defining archetype parameters in the config.

Is this possible? From what I’ve seen it’s not currently accessible from the archetype. Would it be worth adding an issue on github?

Thanks in advance.


#2

It is possible, but “someone” might have been sloppy about documentation. It should be documented in one of the release notes – see the /news section.


#3

Awesome. Thanks for the tip.


#4

I’m having trouble with {{ .Site.Params.twitter }} – did you solve the question? What was the answer? I’m searching in the /news section but I don’t see anything that looks relevant.


#5

Sorry to report I didn’t get very far with it, or I would have probably commented here. In my case it was just a QoL issue and non-critical so I managed to live without but its still a useful feature to have.


#6

This doesn’t exactly pre-fill the front-matter params, but the result is similar.

Assuming that you have this in your site config:

[Params]
  author = "foo"

and expect to override this in the front-matter with:

author = "bar"

you can do this in your templates:

{{ .Param "author" }}

This will:

  • Read the front-matter “author” if set
  • If not, will read it from the site config

You can go even one step further by doing:

{{ .Param "author" | default "blah" }}

That will use the value “blah” if neither site config nor the front-matter has the “author” param defined.


Also, note from:

The Param method may not consider empty strings in a content’s front matter as “not found.” If you are setting preconfigured front matter fields to empty strings using Hugo’s archetypes, it may be best to use the default function instead of Param.

So…

{{ .Params.author | default .Site.Params.author | default "blah" }}

or

{{ .Params.author | default (or .Site.Params.author "blah") }}

#7

The question is not clear… hopefully the answer I just wrote answers that :slight_smile:

If not, please start a new thread with:

  • Better formed question… what’s the “trouble”
  • Post a link to an online git repo with your site source.

#8

Have you actually written an archetype with the code you posted? That doesn’t work for me, as acknowledged by the OP and @bep. I think it should be kept in this thread. You’re right that I wasn’t very specific about the issue. I’ll be more clear because I think the thread in general isn’t super clear and it should be:

bash-3.2$ cat archetypes/wombat.md
---
{{ .Site.Params.twitter }}
---
bash-3.2$ hugo new wombat/newpost.md
Error: Failed to process archetype file "/Users/baron/repos/xaprb/xaprb-src/archetypes/wombat.md": template: wombat:2:8: executing "wombat" at <.Site.Params.twitter>: can't evaluate field Params in type *hugolib.Site

I’ve found that I can’t use most .Site. syntax in archetypes. I can only use file-related ones. If there’s a way to use site-related variables, I’d like to know, because it would be useful to me.


#9

No. I actually don’t use archetypes ever. And so I posted an alternative with this disclaimer:

This doesn’t exactly pre-fill the front-matter params, but the result is similar.

The solution I posted goes in the layouts (not archetypes).

It basically uses an exactly named param in the site config as the default if that is not set as the page front-matter. So… you simply skip setting a front-matter param altogether if you intend to fallback to the site config version.