How to change the layout to single for a _index.md page?

Hi hugo community

How is it possible to change the layout of a _index.md to another layout than list like single?

I would love to change the layout of the two imprint _index.**.md files. I’ve tried to do that with the frontmatter parameter layout, but I didn’t succeed.

---
title: "Impressum"
draft: true
layout: single
---

What am I doing wrong?

Thank you for any advice :slight_smile:

Robin

You are looking for the template lookup order. Every single page on your site can have a unique template (but obviously we keep it DRY by default).

You can also use layout to use an existing, appropriate template, but you’ll still need to understand the lookup order and which templates apply to which views to use it correctly. :slight_smile:

Thank you for your answer!

I’ve seen this page, but probably I don’t understand it. I have this _index.md file and I want that this file is rendered with layouts/_default/single.html from the theme. What do I need to do?

I know _index.md files are usually rendered with the list.html layout. How can I override this behavior?

1 Like

Nope. The reason is each kind of page created has different variables available. So you will need to copy over that template to where it is being picked up, and modify the code if needed. :slight_smile:

Just copy the contents of single.html to /layouts/section/imprint/list.html. That should do it. Or even better if you want to follow the DRY principle. Place the contents of single.html in a partial.

Then call this partial in both templates. So that in the future if you need to change something you only have to do it once.

1 Like

You can also use

layout: layoutname

in Front Matter of the page. Just save your individual layout in layouts.

Thank you for your answer. I’m already using layout: single in my FrontMatter declaration, but my themes/theme/layouts/_default/single.html is not used for rendering. Instead themes/theme/layouts/_default/list.html is used.

It seems to be odd somehow. Why does the FrontMatter parameter layout not work on _index files? Where can I read about this in the documentation?

FYI, the docs have a search bar at the top. You can search for things like “layout” or “front matter”.

I actually think two things are happening here, and they are not the same, but I want to explain it so you get how this works.

First of all, check the doc for defining a content type, it has information about this. Included is a note:

With the “everything is a page” data model introduced in v0.18 (see Content Organization), you can use _index.md in content directories to add both content and front matter to list pages. However, type and layout declared in the front matter of _index.md are not currently respected at build time as of v0.19. This is a known issue (#3005).

So that should explain why you can’t define the layout in an _index.html.

However, there is something else to understand about the lookup orders: a list page will never look for a single.html template, because it does not show up in the the lookup order.

If this isn’t obvious, please look through each lookup order and it might make sense. :slight_smile:

2 Likes

Thank you @maiki for the patience to explain me this in all details. :slight_smile:

1 Like