Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hugo won't render single page layout, instead uses _default single layout

Tags:

hugo

)

I'm trying to create my own theme for hugo.io. So far, everything works fine. My only problem is, that I can't get hugo to render the custom layouts for single section content.

For posts this works fine:

  • The index.html gets called correctly
  • The single.html inside the "layouts/posts" gets called correctly
  • For tutorials, the list layout works correctly
  • The permalinks for all files and lists work correctly

The problem: * For tutorials, the single layout doesn't call the "layouts/tutorials/single.html" layout. Instead it uses "layouts/_default/single.html"

I tried the following:

  • add type= "tutorial" preface setting to all tutorial .md files
  • add layout= "tutorial" preface setting to all tutorial .md files
  • add a "tutorial.html" file inside the "layouts/_default" folder (also tried naming it "tutorials.html)
  • add a "tutorial.html" file inside the "layouts" folder (same as above)

sadly, none of this works

My setup of content:

content
|__ posts
   |__ new_post.md
|__ tutorials
   |__ new-tutorial.md

My setup of layouts (inside my theme folder):

layouts
|__ _default
   |__ list.html
   |__ single.html
|__ posts
   |__ list.html
   |__ single.html
|__ tutorials
   |__ list.html
   |__ single.html
|__index.html

My single tutorial content (new-tutorial.md):

+++
title = "My new Tutorial"
date: 2019-10-04T14:10:46+02:00
draft: false
type: tutorial
layout: tutorial
+++

# Custom Content Headline

There is no error message from hugo. I expect hugo to open the page "http://localhost:1313/tutorials/new-tutorial/ with the layout that lies in the file "layouts/tutorials/single.html"

like image 940
Shiny Taris Avatar asked Dec 06 '25 19:12

Shiny Taris


2 Answers

You should not specify type: tutorial and type:tutorial in new-tutorial.md in your case.

Hugo will use layouts/tutorials/single.html by default for tutorial section, see https://gohugo.io/templates/lookup-order/#examples-layout-lookup-for-regular-pages

Final result for new-tutorial.md will look like

+++
title = "My new Tutorial"
date: 2019-10-04T14:10:46+02:00
draft: false
+++

# Custom Content Headline
like image 102
Pavel Kovalev Avatar answered Dec 10 '25 08:12

Pavel Kovalev


Already solved the issue, the problem is in the type definition. Hugo by default gives the elements inside the tutorials folder, the type tutorials, if you change it to tutorial, you must make a subfolder with the tutorial htmls, that are the new type.

layouts
|__ _default
   |__ list.html
   |__ single.html
|__ posts
   |__ list.html
   |__ single.html
|__ tutorials
   |__ list.html
   |__ single.html
|__ tutorial
   |__ list.html
   |__ single.html
|__index.html

like it says in the documentation https://gohugo.io/templates/views#which-template-will-be-rendered In case I am not making my self clear, you can read about it here and here

like image 29
jacobitosuperstar Avatar answered Dec 10 '25 09:12

jacobitosuperstar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!