How to override a site param with a section param?

Is there a way to override a site parameter (defined in config.toml) with a section parameter (defined in content/SECTION/_index.md)?

My goal is to be able to define a setting globally and override it per content-type (NOT just for a specific piece of content). Kind of what {{ $.Param "foo" }} does, only it does it for site and page parameters, and I’d like to do it for site and section parameters.

Put it another way, I’d like to access the params defined in content/SECTION/_index.md from the layouts/SECTION/single.html template (yes, from single.html — NOT from list.html).

Thank you.

I need more details to give you a full solution, but I don’t see why you couldn’t do this with the templating…

{{ if eq .Section "matchedsection"}}
  {{.Params.pageparm}}
    {{ else }} 
  {{ .Site.Params.siteparam }}
{{ end }}

If you’re looking for values specifically set in an _index.md file, you can also use the GetPage function.

1 Like

You could also possibly use the default function, but I’d still need more specifics of what you’re trying to accomplish…

2 Likes

Default is what comes to mind to me, too. So, this is something I’ve used: {{ .Title | default .Site.Title }}

1 Like

Thanks for your answer.

Here’s what I’m trying to accomplish. In my website I have several settings affecting how the content is displayed, e.g. whether comments are enabled, whether social sharing is enabled, etc. In my experience, it is extremely convenient to be able to define these settings at three different levels:

  • Site-wide
  • Per content-type
  • For a specific piece of content

Which — as I understand it — translates to the following Hugo concepts:

  • Site parameter defined in config.toml.
  • Section parameter defined in the front matter of content/SECTION/_index.md.
  • Page parameter defined in the front matter of content/SECTION/SOME_CONTENT.md.

Now within a specific piece of content, I want to use one of those three values, from most specific to least specific (same rationale that Hugo uses to determine which template to use to display a given page).

Example : My individual posts are displayed with layouts/post/single.html. In this template I need to know whether comments are enabled for the current post. I want to use ONE of the following values, IN THIS ORDER:

  • Value of the comments_enabled param defined in the front matter of content/post/hello.md (= the current post). Or, if not found:
  • Value of the comments_enabled param defined in the front matter content/post/_index.md (= the post content-type). Or, if not found:
  • Value of the comments_enabled param defined in config.toml (= side-wide param).

As soon as a value is found, we can stop searching.

Disclaimer: I’m new to Hugo and heavily influenced by the Drupal CMS. I might have gotten some of this wrong. :sweat_smile:

How about something like this?

{{ if isset .Params "comments_enabled" }}

    <!-- do what needs to be done when the post's comments are enabled in the content's front matter-->

{{ else if in "/blog/ /articles/ /topics/" .Section }}

    <!-- do what needs to be done with the comment when the 
        current post is in the /blog/, /articles/, and /topics/ settings -->

{{ else if .Site.Params.comments_enabled }}

    <!-- do what needs to be done when the 'comments_enabled' setting below
    the '[params]' heading in config.toml is set. -->
    
{{ end }}

(Note: this is untested since my website differs too much from how you’ve structured things.)

Hey Jura. That’s the idea, yes,

But how would you retrieve the value of the comments_enabled param for the current section (that param being defined in the front matter of content/SECTION/_index.md)?

The 2nd block in your code does something based on what the current section is. I want to do something based on the value of a given param for the current section.

I you define a param “myParam” in both site config and in /mysection/_index.md, then:

  • From the section listing template: .Param "myParam" will return the value from /mysection/_index.md
  • From a single page template inside /mysection: .Param "myParam" will return value from site config
  • If you want to, say, get the behaviour from the first example in the home page template, you can do `.Site.GetPage(“section”, “mysection”).Param “myParam”````

.Site.GetPage doesn’t do any caching at the moment, so if used too much, it may hurt performance (we will fix that)(you may also want to pull it out into a variable if you need more than one param), but the above should work.