Separate render hooks/templates for different output formats

I wanted to post my finding here as I spend quite a few days figuring this out. Still, I want to thank this post as it put me on the right track.

If you want to apply separate logic to the different output formats you use, you can do that by creating separate render-hook or template files under layouts. For example, to change how headings’ autolinks are rendered, create:

layouts/_default/_markup/render-heading.html
layouts/_default/_markup/render-heading.xml

So far so good. In the case of the Docsy theme, however, it creates a custom output format called print, defined as follows (in config.toml):

[outputFormats]
[outputFormats.PRINT]
baseName = "index"
isHTML = true
mediaType = "text/html"
path = "_print"
permalinkable = false

I assumed I needed to create layouts/_default/_markup/render-heading.print to customize it, but I was wrong. I needed to use the following name instead:

layouts/_default/_markup/render-heading.print.html

I’d appreciate any further input from Hugo maintainers explaining the full logic behind building these filenames.

Hi @gvallverdu , are you working on fixing the internal links in the docsy print output, or is that for something else? (If it is, then a big thank you is in order :D)

Yes, I am :slight_smile:, and I’m almost there. In fact, I have a working solution, but it relies on a shortcode I’m working on that does links using a different format. Give me a few weeks so I can figure out if I can do the job without that shortcode.

In the meantime, if you can give this Docsy GitHub requests a thumb up, that would be great!

1 Like

Awesome, many thanks for your work!

https://gohugo.io/templates/output-formats/#template-lookup-order

1 Like

Thanks, jmooring!

fekete-robert, I posted my solution for the Docsy print page links here: More Useful Links in "Print entire section" Mode · google/docsy · Discussion #2144 · GitHub.