Hugo change layout
Asked Answered
J

1

10

I have a following structure in my Hugo & Doks project:

 - content 
   - docs
      - working.md
      - working-as-well.md
   - get-started2
      - working.md
      ...
   - get-started.md
...
  - layouts
    - get-started2
      - single.html
    - get-started.html

Content placed in docs and get-started2 has customized layout and works as expected. Great!

However, I have huge pain to set-up custom layout for get-started.md. I cannot do it like with get-started2 - place it into a folder. Client requests to have get started URL as:

www.SomeProject.com/get-started

so it must not be placed in folder (like get-started2)

www.SomeProject.com/get-started2/working

Do you have any hints please?

Jacquie answered 8/2, 2022 at 9:17 Comment(0)
T
19

You have at least three options:

Use front matter to set the "type":

In content/get-started.md set type: mytype in front matter.

The layout will be located at (for example) layout/mytype/single.html.


Use front matter to set the "layout":

In content/get-started.md set layout: mylayout in front matter.

The layout will be located at (for example) layout/_default/mylayout.html


Or use a subdirectory, but make it the section index:

The file is located at content/get-started/_index.md

The layout will be located at (for example) layout/get-started/list.html

Notice that the template name has changed from "single" to "list", but you can still write your layout in exactly the same way.


I use "(for example)" because Hugo has an enormous list of directories/filenames it searches through to find its templates, and if an earlier matching template is found, the custom layout gets ignored.

Turbinate answered 8/2, 2022 at 11:48 Comment(3)
I did the tutorial and the folder layouts was created and theme is ananke. How can I use frontmatter with layout: test? Easier would be to remove the theme? Can you adapt your answer to include an example related to the tutorial?Amphibology
I've been using hugo for 2 years and I wasn't clear on the above - very much appreciated Richard.Washout
If the content: content/global/en.md what are the layouts for it only for this node?Micronucleus

© 2022 - 2024 — McMap. All rights reserved.