Is it possible to pass custom frontmatter parameters through "hugo new" CLI command?

for example a docs archetype with frontmatter:

---
title: "{{ replace .Name "-" " " | title }}: {{ .SubTopic | title }}"
...
---

with the ability to run the following command:

hugo new docs/topic-name --custom-param SubTopic=something

would generate the file:

---
title: "Topic Name: Something
...
---

No, it is not possible to set a custom front matter parameter through the CLI.

thats a bummer man. thank you for the reply

call hugo new from a script and let the script do the insert

Yes, it is possible.

archetypes/default.md

---
title: "{{ replace .Name "-" " " | title }}: {{ getenv "SUBTOPIC" }}"
date: {{ .Date }}
draft: false
---

Then set the environment variable before the command:

SUBTOPIC="Hello World!" hugo new docs/topic-name.md

This produces the following in content/docs/topic-name.md

---
title: "Topic Name: Hello World!"
date: 2021-03-17T19:23:58-07:00
draft: false
---

See https://gohugo.io/functions/getenv/.

2 Likes

Bonus points:

archetypes/default.md

---
title: "{{ replace .Name "-" " " | title }}: {{ getenv "SUBTOPIC" }}"
date: {{ .Date }}
draft: false
subtopic: "{{ getenv "SUBTOPIC" }}"
---

Now you can access the value from your templates with {{ .Params.subtopic }}.

4 Likes

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.

cc: @bep

2 Likes

Iā€™m pretty sure this is what the OP asked for.

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.

This is a question for the project maintainer.

I do not currently contribute to the Hugo code base because my knowledge of Go is pretty basic.

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

Or none:

hugo new post/test.md

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