One List Layout For Different Section?

Dear All

I have 3 section inside /content/ folder.

/content/
| _ /blog/ (all blog posts with _index.md)
| _ /poem/ (all poem posts with _index.md)
| _ /quote/ (all quote posts with _index.md)

On the /layouts/_default/ folder, I have list.html with content of all 3 section above.

I want _index.md of each folder only display each of its section.

So, _index.md inside blog folder only displaying posts from blog folder. And _index.md inside poem folder only displaying posts from poem folder. Also _index.md inside quote folder only displaying posts from quote folder.

How to solve this? Or I have to create different list layout for each section?

Regards

You can create blog, poem and quote directory inside layouts then add list.html on each of content section

layouts/
      blog/list.html
      poem/list.html
      quote/list.html

if its done then you have to range the list according to their type, for example

{{ $paginator := .Paginate (where .Data.Pages "Type" "blog") }}
    {{ range $paginator.Pages }}
    ...

Semoga membantu

My current method is create 3 deifferent layouts (listblog.html, listpoem.html, listquote.html) inside /layouts/_default folder. I don’t know which one is more efficient result between your suggestion or my current method?

By the way, terima kasih.

I guess its depend on your needs, but imo having different layout directory will make theme easier to manage, specialy if i have different template (single, list, custom uptout etc) for each content.

1 Like

If you in layouts/_default/list.html have code like this:

{{ $paginator := .Paginate (.Pages) }}
{{ range $paginator.Pages }}
…
{{ end }}

The template will list all pages within the current section.

On the home page it will list all pages from all sections. Adding a layouts/index.html template allows you to have a custom view for the home page.

1 Like

Great to know if list.html (along with the code above) can filter section folder automatically. I will try that method. Thanks.

Hello, @frjo

I tried to use your code above, but there’s a failed building on Netlify.

Error: add site dependencies: load resources: loading templates: "/opt/build/repo/layouts/_default/list.html:16:1": parse failed: template: _default/list.html:16: undefined variable "$paginator"

I don’t know which code is wrong. So, I reverted back to the default theme code here ->

But the problem with the default code above is, the result only displaying posts from Poem section only, without displaying posts from Blog and Quote section?

https://mypaper.netlify.com/blog/

Can you help me to correct my list.html above?

The layouts/_default/list.html should be renamed/moved to layouts/index.html, it at least look like what you want on the home page and not on default list pages.

I would then put something like the following in a new layouts/_default/list.html:

{{ partial "head.html" . }}
{{ partial "header.html" . }}

<main class="main">

<h1>{{ .Title }}</h1>

{{ $paginator := .Paginate (.Pages) }}
{{ range $paginator.Pages }}
    <h2>{{ .Title }}</h2>
    <p>{{ .Summary }}</p>
{{ end }}

</main>

{{ partial "footer.html" . }}

Also rename layouts/_default/post.html back to layouts/_default/single.html and that will be the default template for single view.

If you want custom templates for a section you can put that in layouts/section_name_here/single.html.

1 Like

I got it. Thanks, Fredrik.