Same layout for different content types

I have the following structure for my content.

content
-- notes
---- note1.md
---- note2.md
-- links
---- link1.md
---- link2.md
-- posts
---- post1.md
---- post2.md

I do understand how Hugo determines which layout to use. However. I struggle how to use the same layout for links and posts. I already have a layout for posts and want to reuse it for links without having to add type and/or layout to every file within links. As I also want links and posts to be separate content types I don’t think that using type would be a good idea anyways.

Is there any way to do this?

Thanks.

take the solution from here. just adapt the layout and target

hugo.toml
[[cascade]]
    layout = 'posts'
  [cascade._target]
    path = '/links/**'
    kind = 'page'

Even though I do see how it should be working, somehow it doesn’t on my end.

As I’m using YAML instead of TOML I tried adding the following

cascade:
  - _target:
      kind: page
      path: /links/**
    layout: posts

to either my hugo.yaml or /content/_index.md.

In both cases I could still see the template being using for content/links/link1.md was layouts/_default/single.html in contrast to my expectation of layouts/posts/single.html being used.

What am I missing?

Thanks.

If you want links and posts to use the same template for single pages, and you want to retain their content type:

  1. Create a template named β€œfoo.html”

    layouts/
    β”œβ”€β”€ _default/
    β”‚   β”œβ”€β”€ baseof.html
    β”‚   β”œβ”€β”€ foo.html    <-- this is our single template for links and posts
    β”‚   β”œβ”€β”€ home.html
    β”‚   β”œβ”€β”€ list.html
    β”‚   └── single.html
    β”œβ”€β”€ partials/
    └── shortcodes/
    
  2. Add this to your site configuration:

    [[cascade]]
    layout = 'foo'
    [cascade._target]
    kind = 'page'
    path = '{/posts/**,/links/**}'
    

    Or as YAML:

    cascade:
      - layout: foo
        _target:
          kind: page
          path: "{/posts/**,/links/**}"
    
1 Like

That works as described. :slightly_smiling_face:

But maybe I wasn’t specific enough. I’d rather have all the same layouts for posts and links, not only for single pages but at the same retain their content type. Is this possible as well?

OK, so we’ve taken care of the single page template.
Now do the same thing for the list page template.
But with more descriptive names.

layouts/
β”œβ”€β”€ _default/
β”‚   β”œβ”€β”€ baseof.html
β”‚   β”œβ”€β”€ home.html
β”‚   β”œβ”€β”€ links-and-posts-list.html
β”‚   β”œβ”€β”€ links-and-posts-single.html
β”‚   β”œβ”€β”€ list.html
β”‚   └── single.html
β”œβ”€β”€ partials/
└── shortcodes/

And the site configuration would look like this:

[[cascade]]
layout = 'links-and-posts-single'
[cascade._target]
kind = 'page'
path = '{/posts/**,/links/**}'

[[cascade]]
layout = 'links-and-posts-list'
[cascade._target]
kind = 'section'
path = '{/posts,/posts/**,/links,/links/**}'

Or in YAML:

cascade:
  - layout: links-and-posts-single
    _target:
      kind: page
      path: "{/posts/**,/links/**}"
  - layout: links-and-posts-list
    _target:
      kind: section
      path: "{/posts,/posts/**,/links,/links/**}"
1 Like

Thanks. With the latest change it still works for page but not for section. It doesn’t work, even though I have both, the cascade entry and corresponding file in _default.

Any idea what might be causing this?

I updated the TOML and YAML in my previous post so that it would catch the top level sections.

I have no idea what else your site has, but it might be easier to use the default list and single templates for your links and posts (since they share the same templates), and create unique templates for the other content types, taxonomies, and terms.

Fair point. It’s actually quite a good idea.
Your latest change did make it work for posts top level section for whatever it’s still not working for links top level section. Anyhow, I do have enough feedback/solution so that I know what to tinker with going forward.
Your latest change is working and I also found why it wasn’t initially working for links. I still had an _index.md in links which set a layout from my earlier experiment.s

Many thanks.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.