Why frontmatter in config doesn't support custom params?

I want to set default value for frontmatter in each files and I thought that I could just create a frontmatter section like this:

And put some custom params in it. But it seems like this doesn’t work. Is there anyway to work around with it?

Would you give an example of what you’d like to do. It’s not clear to me yet

1 Like

My apology, here is extract from my code:

/config/_default/frontmatter.toml

# To display comment
showCmt		= true

/content/posts/hello-world.md

date: 25 Sep 2019, 07:00 +0700
title: Hello World!
summary: "Goodnight World"

/content/posts/hello-world-again.md

date: 25 Sep 2019, 07:00 +0700
title: Hello World... Again?
summary: "I know I say it twice, you don't have to remind me that. (~_~)"
showCmt: false

I expect that it would be like this after compile:

  • hello-world.md -> showCmt[true]
  • hello-world-again.md -> showCmt[false]

Reality when I use debug: <nil>.

I think what you instead is: https://gohugo.io/content-management/archetypes/#readout

Also https://gohugo.io/content-management/front-matter#front-matter-cascade

1 Like

which suggests the question: do params cascade from the site config?

1 Like

@zwbetz I think that what I try to avoid. I want to limit down the params in each file.md as much as possible. I think a default params (the one that will be used when the param in file.md are not declared).

@bep the cascade seems to be what I need, however does it work from the site config (like @HenrySkup mentioned) because I want to set default at the theme scope. Also the term descendants is not quite clear. I don’t know whether it is this:

  • blog/_index.md
    • blog/abc.md

Or like this:

  • blog/_index.md
    • blog/abc/_index.md

Maybe the document need a bit update for this section.

You could just treat the config params as the default values, overriding with a more specific value if it exists:

{{ $myparam := "" }}
{{ if isset .Params "foo" }}
  {{ $myparam = .Params.foo }}
{{ else }}
  {{ $myparam = site.Params.foo }}
{{ end }}

Or

{{ $myparam := .Params.foo | default site.Params.foo }}
{{ $myparam }}
1 Like

or without the pipe

{{ $myparam := default site.Params.foo .Params.foo }}

see

@ju52 @pointyfar Thanks for your suggestion. I’m using it too. But I don’t want abuse params, or more exactly don’t want to have too many params in my config directory. My point in this topic is to try to take advance of frontmatter.toml so that I can be a bit lazy and also reduce a few bytes of each files.md

The thing though is that config/frontmatter.toml is (as far as I understand) not for setting default front matter values. It is for configuring the various dates that are then used in the front matter of your content when you hugo new newpost.md.

All custom config settings are meant to go under params.

If you really want it outside of params, you could use the data folder instead and set your default params there.

1 Like

Yeah, I did use data for some cases. But don’t you think it would be better if frontmatter.toml could work as default values? Anyway, your reply seems like an answer for my question so I will mark solution here. I will post suggestion in github later. It may be waste if frontmatter.toml is used just for date setting, don’t you think so :smiley:

for default values we have archetypes


here my sample for posts

+++
categories  = ["Post"]
tags        = ["{{ now.Format "2006"}}"]
description = "{{ replace .TranslationBaseName "-" " " | title }}"
title       = "{{ replace .TranslationBaseName "-" " " | title }}"
date        = "{{ (.Date |time).Format "2006-01-02T15:04:05Z07:00" }}"
expirydate  = "{{ ((.Date | time ).AddDate 2 0 0).Format "2006-01-02T15:04:05Z07:00"}}"
draft       = false
series      = []
+++
## H2
**Insert Lead paragraph here.**

<!--more-->

Also see https://gohugo.io/functions/merge/#readout

I understand what you want, but that isn’t possible. I understand that it would be useful in some situations, but front matter is just a data/config source. You kind of have to present these data in the templates at some point, which is where you can put your merge/default logic.

2 Likes

It cascades from the Homepage if I recall. Works pretty good for your default, as the homepage is (again only 80% sure) the parent of all pages.

2 Likes

Yes, and yes.