How to set default frontmatter (without using the new parameter)

Hello there,

I am looking to set frontmatter variables to a default value for a particular layout.

My understanding is that the normal way to do this is to have an architype file that stores the defaults for the frontmatter. Then when I create a new page, hugo will use those as the defaults.

My issue is;
What if I want to change the defaults for all markdown files in a directory or layout at once? or…
If I want to leave the frontmatter parameter empty, how can I set it according to defaults somehow?

Is there a way to do that?

Thanks heaps for any advice

1 Like

I’m not 100% I understand your question. Do you want to create a different archetype for a particular section/directory of your site, so that new posts will have different frontmatter? Or do you want to do a mass update/change of frontmatter for already existing posts?

Hi funkydan, thank you for asking further clarification.

Do a mass update/change of frontmatter for existing posts? No.
Create a different archetype for a particular directory of my site? Yes, well sort of…but without the archetype…

Perhaps the best way to phrase what I want is: How do I set default values for frontmatter if they are not set or omitted from the markdown file?

Here is an example of what I want to do;
For a section of my website, I display a comment form at the bottom of the text. Most of the time I want to show it, sometimes I don’t. So I want to default it to always show it, unless I specifically set the frontmatter value to true.

I know I can just set that in my layout folder were I do isset(myparameter) etc…but I was just wondering if there is a more proper or common practice way to do it?

Thanks for any thoughts
John

I can’t think of any problems with the way you’re thinking.

Since the front matter is a boolean value, if it’s not present, then checking for the value will return false… like with draft posts. If draft=false or it’s not set, then the post is published, but if draft=true then it isn’t published (unless you run with the -d flag).

Thanks funkydan2,

I suppose I shall go forth an do it that way…which now that I think about it a bit more clearly, seems ok…

I think I was initially confused about what architypes were for. I thought they were the way to set things default. But now I know they are just for creating new content with the preset frontmatter…and that is their only purpose (that I know of)

Thank you once again, everyone here is so helpful, hope I can repay…
John

So you want to show the form only if a front matter param exists thats pretty easy to do if you wrap your form with an isset statement.

Thanks Benmarte!

Yeah that’s the way I’ve ended up going…I sort of just wondered if there was another way…but I think I was just initially confused with architypes and what their use was for…so perhaps in retrospect this was a silly question :slight_smile:

There is no silly questions so don’t think that.

One of the things I love about this forum is the many different ways you can do things with hugo, no matter how silly it may seem you always learn something new.

I use shortcodes to insert different things.
Make a comment.html shortcode and put it in the page, where you want it.
{{< comment >}}

with {{ $comments := "true" }} in the shortcode.

This shortcode can set a variable and this setting can used to activate the comment section.
Create a partial with {{ isset $comment }}