Allow users to opt-out of your internal template override

Let’s say you have a spiffy override for an internal Hugo template but you want your users to be able to choose whether to use that override or not. The following example is for allowing a user to opt-out of using your phenomenal new RSS feed template and use the internal default.

Put this in layouts/_default/rss.xml and your override in layouts/partials/internal-overrides/rss.xml and if the user sets internalTemplateOverrideRSS = false in config.toml then they will get the Hugo default behavior.

{{- if ne (.Site.Params.internalTemplatesOverrideRSS | default true) true -}}
    {{- template "_internal/_default/rss.xml" . -}}
{{- else -}}
    {{- partial "internal-overrides/rss.xml" . -}}
{{- end -}}
{{- /* Remove trailing newlines */ -}}

I hope someone finds this helpful!

4 Likes

What is the difference between calling partial and template here? I have never seen this template function and can’t find it in the documentation.

The template keyword is a built-in Go template keyword, the main difference between that and partial is that template gets expanded at parse-time (I’m sure that description wasn’t very good), partial is a “runtime thing”, a function that returns a value.

This means that you cannot do this{.

{{- $value := template "_internal/_default/rss.xml" . -}}

Etc.

But, similar to partials, they can be defined either via file or inline – but you can only load partials from /partials.

2 Likes

I found out about in reading about internal templates in the docs

but the documentation of the template keyword is in the go doc for the template package (because it is a Go template package builtin rather than particular to Hugo):

I did eventually find a brief mention of it in the Hugo docs. Turns out “template” isn’t a good search term to use against the documentation. :joy:

2 Likes

(post deleted by author)