Render content with two different HTML templates

I would like to take my content in ./content/posts and render it twice with two different templates. I looked at custom outputs but that didn’t seem like what I wanted because both of my templates render html.

I looked into content view templates and this seems closer to what I want but I can’t figure out if it can be used to render pages twice.

I made a custom template for my content called “embed” and what I currently do is move all the content into ./content/embed and then run hugo to render the content into /public. I copy the rendered pages into ./static and then copy my content to ./content/posts and re-render the site.

You can see the rendered output live at posts and embed

Does anyone know if this is natively supported with hugo or should I stick with my current workflow of rendering the content twice?

As far as I know, one can have multiple Output Formats for the same file format.

Simply define the second HTML output as EMBED or something. Then make sure to disable indexing from search engines (if you are concerned about getting duplicate content penalties).

I am not aware of another way that you could achieve what you want.

You can.

Thanks for pointing me in that direction. I re-read the docs and started creating a custom output format. I added this to my config.toml

  page = ["HTML", "EMBED"]

    name = "embed"
    path = "embed"
    isHTML = true

And then added my templates to layouts/posts/single-baseof.embed.html and layouts/posts/single.embed.html

I obviously did something wrong because I see this warning for each page

WARN 2021/05/10 09:21:22 found no layout file for "embed" for kind "page": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.

But also my regular home page and standard HTML templates are all broken. I’ll keep digging into the docs to see what I’m missing or did wrong.

I’m surprised that we don’t throw an error about this, but the output format defintion needs a media type:

mediaType = "text/html"

With the above, Hugo should be able to fall back to single.html etc., but if you want a template for that output format, you can do single.embed.html etc.

You may ask what the isHTML flag is for, but that is a longer story.

Thank you so much! I think that solved the problem for me.

One last question I’m not sure if it’s possible. I see the path is added to the content type (eg embed/posts) is there any way to make the path replace posts in the URL? It’s not a big deal if it can’t but it makes some of my regex easier to match if the word is one or another

It is not currently possible to configure permalinks per Custom Output Format.

The Permalinks Configuration works for sections across all Outputs.

Thanks for clarifying. :grinning:

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