templatesincludehugotemplating

Hugo: Use single.html for index pages


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.


Solution

  • 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.