Layout doesn't work but Type sets the layout---why?

Long story short I’m consolidating various templates into a generic template. This means I am going back over old content and updating which layout each page should have. Whereas in the past, I had a different template for each type, now I am consolidating various functions into a single template across all types. This means I have something like this in my front matter:

---
layout: "mytemplate"
type: "blog"
---

and

--- 
layout: "mytemplate"
type: "funfacts"
---

I am finding that “layout: mytemplate” doesn’t work at all. Only “type: mytemplate” works. But this is a major problem for me because I need to use “mytemplate” across different content types. The result is, Hugo looks up the “type” for the layout, doesn’t find it (because it doesn’t exist, only “mytemplate” exists), and goes to the _default.

When I remove “type” altogether from my frontmatter, to test, I find that “layout: mytemplate” still doesn’t work. So the problem seems to be that “layout” doesn’t work in my frontmatter.

Can someone explain why this might be happening?

Thanks.

What is the location of the template for the above layout under /layouts/?

Also have a look at this informative post from a few years back:

The layout is in

/layouts/mytemplate/

Hugo is loading the _default layouts from the same location.

Hugo will also load “mytemplate” if I set it in “type” rather than “layout”.

Something is causing “layout” not to set the layout.

Thanks.

No. This will not work.

Try changing the filename of the template and place it under:
/layouts/mytemplate/single.html (if this is meant for a single page)
/layouts/mytemplate/list.html (if this is meant for a list page)

Also see:

Surely you cannot expect to get help in this forum with this attitude.

Anyway I have no time for any of this. My time is limited.

Perhaps others can help you.

I’m deleting my account.

Content Type

content
└── post/
    └── post-1.md

content/post/post-1.md

+++
title = "Post 1"
date = 2021-01-01T00:00:00-00:00
draft = false
+++

The content type is not explicitly defined in front matter. The type is inferred from the top level section: post.

Example 1

With this layout structure:

layouts/
└── _default/
    ├── baseof.html
    ├── list.html
    └── single.html     <-- Post 1 uses this 

the “Post 1” page will use layouts/_default/single.html.

Example 2

With this layout structure:

layouts/
├── _default/
│   ├── baseof.html
│   ├── list.html
│   └── single.html
└── post/
    └── single.html     <-- Post 1 uses this 

the “Post 1” page will use layouts/post/single.html.

Example 3

Now let’s specify a different layout for the “Post 1” page:

+++
title = "Post 1"
date = 2021-01-01T00:00:00-00:00
draft = false
layout = "condensed"
+++

This won’t change anything, because we have not created the “condensed” layout yet. The “Post 1” page will still use layouts/post/single.html.

Let’s create the “condensed” layout:

layouts/
├── _default/
│   ├── baseof.html
│   ├── list.html
│   └── single.html
└── post/
    ├── condensed.html  <-- Post 1 uses this 
    └── single.html

Now the “Post 1” page will use layouts/post/condensed.html.

Example 4

Now let’s create a new content type, article.

content/
├── article/
│   └── article-1.md
└── post/
    └── post-1.md
+++
title = "Article 1"
date = 2021-05-08T13:01:07-07:00
draft = false
+++

With our existing layout structure, the “Article 1” page will use layouts/_default/single.html.

Example 5

What if we want “Article 1” to use the “condensed” layout?

+++
title = "Article 1"
date = 2021-05-08T13:01:07-07:00
draft = false
layout = "condensed"
+++

That doesn’t work. The “Article 1” page will still use layouts/_default/single.html. Why? Because Hugo is looking for layouts/article/condensed.html or layouts/_default/condensed.html, neither of which are defined.

What can we do?

Option 1 (Bad Idea)

Explicitly set the content type for the “Article 1” page to post.

+++
title = "Article 1"
date = 2021-05-08T13:01:07-07:00
draft = false
layout = "condensed"
type = "post"
+++

Now the “Article 1” page will use layouts/post/condensed.html.

Why is this a bad idea? Because now both “Post 1” and “Article 1” will appear when we do this:

{{ range where .Site.RegularPages "Type" "post" }}

Option 2

+++
title = "Article 1"
date = 2021-05-08T13:01:07-07:00
draft = false
layout = "condensed"
+++
layouts/
├── _default/
│   ├── baseof.html
│   ├── condensed.html  <-- Article 1 uses this 
│   ├── list.html
│   └── single.html
└── post/
    ├── condensed.html  <-- Post 1 uses this 
    └── single.html

Now the “Article 1” page will use layouts/_default/condensed.html, and the “Post 1” page will use layouts/post/condensed.html.

Option 3

+++
title = "Article 1"
date = 2021-05-08T13:01:07-07:00
draft = false
layout = "condensed"
+++
layouts/
├── article/
│   └── condensed.html  <-- Article 1 uses this 
├── _default/
│   ├── baseof.html
│   ├── list.html
│   └── single.html
└── post/
    ├── condensed.html  <-- Post 1 uses this 
    └── single.html

Now the “Article 1” page will use layouts/article/condensed.html, and the “Post 1” page will use layouts/post/condensed.html.

3 Likes

This topic was automatically closed after 15 hours. New replies are no longer allowed.