Home > OS >  Hugo: Use single.html for index pages
Hugo: Use single.html for index pages

Time:07-19

In my Hugo theme, I use the default layouts/_default/single.html file to render my content pages and the default layouts/index.html file to render all index pages. I don't want to render my index pages differently and would like to have them rendered by single.html.

Of course, I don't want to duplicate the code of single.html in index.html. So, is there a way to to tell Hugo to use single.html also for index pages?

I could move the code from single.html into a partial and include that in both files, but I am not sure if that is the way to do it in Hugo or if there is already another best practice.

CodePudding user response:

I had to make some logical jumps as I think you might not understand hugo templating yet... based on your "index files" comment. So: TLDR; delete index.html in _default and single.html and default and instead have 1 list.html. Should work. Otherwise:

So a few questions compadre.

What do you mean by "index pages" as Only the homepage renders with index.html

https://gohugo.io/templates/lookup-order/
Hugo's lookup order if what you need to read/look at/learn generally.

So, making some jumps logicamente, IF you mean your page branch/leaf bundles (index.md and _index.md)?

Then again, look up your look-up order and you might see /default/list.html as your solutione, but sans /default/index.html - as then list will override - no?

It all depends on what you are trying to do, but first it might be an idea too look up Hugo's basic system/organization in:

https://gohugo.io/templates/introduction/

But the above solution should work - none-the-less.

CodePudding user response:

I ended up adding the following two lines to the front matter of content/_index.md:

type: _default
layout: single

This way, I don't have to put the content of single.html into a partial to include it in two places and I don't need layouts/index.html anymore. Furthermore, I can use this technique for deeper nested _index.md files, too.

  • Related