Outputting Go Templates from Hugo

I’ve recently been using Hugo to generate a mostly static site that is served by Caddy server. Caddy allows for using Go templates similar to Hugo and lets me add a bit of dynamic content to my static site.

Currently, I’m using ReadFile in my layout to read in the template file that I want in my final output. It contains Go template code, and this is the only way I could figure out how to make Hugo ignore the template code and output it to the final production folder.

In the layout, I read in the file like this:

{{ readFile "path/to/file" | safeHTML }}

and the file has something like this in the output which is then processed by Caddy

{{.Include "path/to/file.html"}}

I was wondering if there’s an easier way, some sort of way to make Hugo ignore blocks of Go Templates that will then get output into the file product so they can be read by Caddy.

Just in case anyone else needs this, it seems like this is the easiest way to output Go Template code from Hugo layouts that will then be parsed by Caddy.

Example of outputting a custom header to a page:

{{ `{{ .Header "X-Custom-Header" }}` | safeHTML }}