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

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.

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.

1 Like

Awesome. Thanks for the tip.

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.

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.

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") }}

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.

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.

2 Likes

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.

I get the same error trying to access .Site.Author:

$ cat archetypes/tmp.md    
---
{{- with .Site.Author }}
author: { name: {{ .name | jsonify }}{{ with .email }}, email: {{ . | jsonify }}{{ end }} }
{{ end }}
draft: true
---

$ hugo new tmp/badger.md
Error: Failed to process archetype file "archetypes/tmp.md": template: tmp:2:14: executing "tmp" at <.Site.Author>: can't evaluate field Author in type *hugolib.Site

I’m seeing the same thing in Hugo 0.40.3 in my own archetypes: I’m unable to access .Site.Params in the front matter.

Both @bep’s comment above and the github notes on the implementation
https://github.com/gohugoio/hugo/pull/3607
imply this is possible(?) but in my testing, including @xaprb’s example above, I get the same error:
can't evaluate field Params in type *hugolib.Site

The .Site object seems to allow access to .Site.Pages, strangely, but not to variables like .Site.Author or .Site.Title or anything under .Site.Params. I’ve tried using the .Site.Param function, but it returns the same error.

OK, so it is a mystery that you are the first one to complain about this… This was my mistake, see

https://github.com/gohugoio/hugo/issues/4732

You can do .Site.Info.Params, but you will have to remember to change it back once Hugo 0.41 is out.

1 Like

Thank you, @bep!

Awesome, as of ab02594, my Params.Author example above now works. however, I still cannot use properties on the Date object:

$ cat archetypes/tmp.md 
---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
date: {{ .Date.UTC }}
---

$  hugo new tmp/badger.md
Error: Failed to process archetype file "archetypes/tmp.md": template: tmp:3:14: executing "tmp" at <.Date.UTC>: can't evaluate field UTC in type string

I think this is documented, but .Date is a string (formatted so it can be used in frontmatter). It was probably a bad decision to name it .Date, but no going back on that now.

But you can do

now.UTC

Etc.

Boy do I relate to naming regret! Thanks for the pointer to now; this works:

date: {{ now.UTC.Format "2006-01-02T15:04:05Z" }}