How to define theme's scope default configuration and parameters?

Hi, I want to define some default configuration and parameters in the theme, so that the users don’t need to tweak their site configuration after upgrading the theme. For example, postcss requires that:

  writeStats = true

I append the code snippet to the theme’s config:

// /mysite/themes/mytheme/config/_default/config.toml
  _merge = "deep"
  writeStats = true

But it doesn’t work, there is no hugo_stats.json in site root.

It works after appending the following code to the site’s config:

// /mysite/config/_default/config.toml
  _merge = "deep"

Am I doing it in a wrong way?

Yes. You are doing it wrong :wink: Please post your whole configuration. In the most cases this is an issue with the order of those [sectionname] sections. The fact that you add an additional [build] and it miraculously works tells me that you have an issue with the order of those sections. Maybe the [build] is not in root, maybe it’s overridden somewhere along the line. TOML is quite rigid in regards to this. And Hugo offers so many ways to configure that you might overlook something.

If you have a config.toml in your root with a build section, this will override the one in config/production/config.toml. If you have a config/_default and a config/production putting something into production will work on the server, but not locally on hugo server (because locally it’s “development”, not “production”).

etc… etc… etc…

Either check your whole config or let us have a look at it.

Theme config(/mysite/themes/mytheme/config/_default/config.toml):

_merge = "deep"
writeStats = true

Site config(/mysite/config/_default/config.toml):

baseURL = ""
title = "Hugo Bootstrap"
theme = "hugo-theme-bootstrap"
copyright = "Copyright © 2016-{year} Razon Yang. All Rights Reserved."

# Multilingual mode
defaultContentLanguage = "en"
defaultContentLanguageInSubdir = true # If you use only one language comment this option
#disableLanguages = ["fr", "zh-cn" ,"zh-tw"] # For disable one or more language

# Pagination
paginate = 10
# paginatePath = "page"

# Disqus
# disqusShortname = "yourdiscussshortname"

# Google analytics
# googleAnalytics = "UA-123-45"

enableRobotsTXT = true

enableEmoji = true

# hasCJKLanguage = true # Turn on it if your content contains Chinese/Japanese/Korean Languages.

pygmentsUseClasses = true

  hrefTargetBlank = true

    suffixes = ["json"]
    name = "manifest"
    baseName = "manifest"
    mediaType = "application/manifest+json"

  home = ["HTML", "RSS", "JSON", "MANIFEST"]

  category = "categories"
  series = "series"
  tag = "tags"

I’m sure that there is no extra [build] section.

// theme's config
writeStats = true

// site's config

The following configuration works as expected, it doesn’t work as expected after changing writeStats to false in theme’s config:

// theme's config
writeStats = false

Does the _merge only take effect in site config?

My expected configuration as follows, but it doesn’t work.

// theme's config
writeStats = true

// site's config
// nothing need to tweak

I think that the _merge is only working from the place it is downwards, meaning if a module imports a module then the _merge is used. You as the user of a theme must have the upper hand in these settings, so having a theme override your local setup is not secure. I have the write stats setting in my own configuration on the root level at config/_default/build.toml with only

writeStats = false

in that file.

Then, the whole merge thing is a CAN not MUST feature. leave it out if you want it to work as default is set. See the docs here for defaults:

1 Like

Thank you. I think I misused this feature.

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