Include resources conditional on frontmatter key

I have a Hugo site with a custom theme for a simple blog.

Some posts require KaTeX (js + css) to display math equations, but I would like this code not to be loaded on all pages, only those posts which have a certain key-value pair in the frontmatter (e.g. katex: true).

Is it possible to do this somehow?

You simply wrap the KaTeX assets within a condition:

{{ if eq .Params.katex true }}
...
{{ end }}
OR
{{ with .Params.katex }}
...
{{ end }}

The first check if the parameter is set to true (boolean).
The second checks for the existence of the katex parameter.

Read more about Conditionals at the documentation:

2 Likes

Awesome, thank you! That solved it.

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