How to directly query a Hugo config value within a template

Is it possible to retrieve the value of a Hugo configuration option like defaultContentLanguageInSubdir from within a template?

A while ago it was asked specifically about retrieving the value of defaultContentLanguageInSubdir. The answer in this particular case would have been to rely on site.LanguagePrefix to build the URLs.

But I wonder why it doesn’t seem possible to directly query a Hugo config value. This is relevant for config keys that are not custom params and have no (direct) representation in site variables like defaultContentLanguageInSubdir. There are lots more it appears to me, e.g. it would allow to test if buildDrafts or buildFuture is active without relying on workarounds.

When we expose a value to the templates we are making an API promise: “Here’s this new thing. Use it to do xyx. And you can be confident that we won’t change how it behaves in the future.”

In short, it locks us into something, which may prevent us from doing something else in the future without first going through deprecation/sunset process.

So we expose what is needed to satisfy common requirements, but push back on others.

Regarding the use case described in the related topic, do it this way:
https://discourse.gohugo.io/t/hugo-v0-112-0-new-template-functions/44512

The punchline of the above topic is:

{{ strings.TrimPrefix hugo.WorkingDir .Page.File.Filename }}
2 Likes

And we do this stuff fairly often, most commonly is that we change bool config settings into structs if we need more fine grained config for a particular topic. It’s easy to maintain backward compability on the config level, but it would be (almost) impossible if these settings were also used in thousands of templates in the wild.

Thanks for the explanations! I understand. And got yet a better idea about how careful Hugo’s architectural choices are made.

So the answer to my first question is simply no, I guess.

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