With all due respect @jmooring it seems to me that your approach is more of a workaround that would require from the user, either to constantly change the subtopic environment variable in the config or set different archetypes per subtopic value.
The original question -at least as I understood it- was about setting a front-matter param through the CLI dynamically on an ad-hoc basic, which is not possible -as I replied above-.
Perhaps this topic merits a feature request.
I am willing to open a new GitHub issue about setting a custom front-matter parameter through the CLI, if this feature is something that could be added to the current Hugo milestone.
to be honest i thought it was already a feature! it seems really useful - at least for my use case. i am not familiar with go, do you think it is a complex addition?
definitely a neat solution man. thank you. this will work great.
i do hope it is possible to add this as a native feature. i see a lot of potential for it especially if vars can be accessed directly in .Params during creation without needing to be copied into the frontmatter.
You can also set arbitrary parameters in front matter, either single or multiple.
To set a single arbitrary parameter in front matter:
archetypes/default.md
+++
title = "{{ replace .Name "-" " " | title }}"
date = {{ .Date }}
draft = false
{{- if and (getenv "KEY") (getenv "VALUE") }}
{{ getenv "KEY" }} = {{ getenv "VALUE" }}
{{- end }}
+++
Then:
KEY="foo" VALUE='"Hello World!"' hugo new post/test.md
This produces:
+++
title = "Test"
date = 2021-03-18T09:17:05-07:00
draft = false
foo = "Hello World!"
+++
To set numeric or boolean values, omit the double quotations.
KEY="foo" VALUE='123' hugo new post/test.md
KEY="foo" VALUE='true' hugo new post/test.md
To set multiple arbitrary parameter in front matter:
archetypes/default.md
+++
title = "{{ replace .Name "-" " " | title }}"
date = {{ .Date }}
draft = false
{{- if and (getenv "KEYS") (getenv "VALUES") }}
{{- $keys := split (getenv "KEYS" ) "|" }}
{{- $values := split (getenv "VALUES") "|" }}
{{- if ne (len $keys) (len $values) }}
{{- errorf "The number of VALUES must match the number of KEYS."}}
{{- end }}
{{- range $k, $v := $keys }}
{{ $v }} = {{ index $values $k }}
{{- end }}
{{- end }}
+++
Then:
KEYS="foo|bar|baz" VALUES='"Hello World!"|123|true' hugo new post/test.md
This produces:
+++
title = "Test"
date = 2021-03-18T09:22:21-07:00
draft = false
foo = "Hello World!"
bar = 123
baz = true
+++
Even though this handles multiple arbitrary parameters, you can also use it to set just one:
KEYS="foo" VALUES='"Hello World"' hugo new post/test.md