Pagination not being generated

I have news.html with the contents

{% extends "base.html" %}

{% block content %}
<div class="post-list">
    <h1>All posts</h1>

    {% for year, posts in section.pages | group_by(attribute="year") %}
    <h2>{{ year }}</h2>

    <ul>
        {% for post in posts %}
        <li><a href="{{ post.permalink }}">{{ post.title }}</a></li>
        {% endfor %}
    </ul>
    {% endfor %}
</div>
{% endblock content %}

but posts don’t show up. My Contents folder looks something like this:

contents/
├ _index.md
└ news/
  ├ _index.md
  └ 2019
    └ 08
      └ post.md

and so on. contents/news/_index.md looks like this:

+++
title="News"
template="news.html"
sort_by="date"
+++

(As a side note, I can’t reference {{ page.title }} from news.html without getting Reason: Variable page.title not found in context while rendering 'news.html' despite the fact I set the title.)

Pagination is not enabled in your example: https://www.getzola.org/documentation/content/section/#pagination but in your case if you want to follow that structure, you will need to create a _index.md in each folder and add transparent to it to pass it up to the news section. The section in https://github.com/getzola/zola/tree/master/test_site/content/posts is pretty much your end goal, look at the 2018 folder to see transparent.

news.html is your section template so there isn’t a page variable, hence the error, try {{section.title}}

It still isn’t working. I flattened things a bit so the directory looks like this now:

contents/
├ _index.md
└ news/
  ├ _index.md
  └ 2019-08-3_post.md

The front matter of news/_index.md is

+++
title = "News"
template = "news.html"
paginate_by = 10
sort_by="date"
+++

Changing it from {{page.title}} to {{section.title}} fixed that issue, thanks!

Aha! Changing line 7 of news.html from {% for year, posts in section.pages | group_by(attribute="year") %} to {% for year, posts in paginator.pages | group_by(attribute="year") %} gets it working.

One more question, can I access it this from multiple places? Like, the 5 most recent posts on the main page, the paginator on /news, and an archive page that has every single post in a list. I tried duplicating what I have in news.html in my index.html for the main page, but no luck.

One more question, can I access it this from multiple places?

You can call get_section (Overview | Zola) which will get the section with the pages unpaginated.
For the archive, it isn’t currently possible.

I tried using

    {% set recent_changes = get_section(path="news/_index.md") %}
    {{ recent_changes }}

but it only returns [object] on the rendered html. Do I have to use for like with the pagination?

Shame about the archive, I guess the pagination will be enough.

Yep, this returns a section: Sections and Pages | Zola and you want to iterate on recent_changes.pages in that template

That works now, but I can’t figure out how to limit it to X posts. It grabs every single one, and I don’t think I can use paginate_by to limit it? I tried {% if loop.index > 5 %} inside {% for page in recent_changes.pages %} but no luck.

Edit: I should have been using < (less-than), not > (greater-than). {% if loop.index < 5 %} does what I want!

Based on the stuff above I figured out how to create an archive though! For anyone in the future looking to do the same thing, here’s what I did.

archive.html:

{% extends "base.html" %}
{% block title %}{{ section.title }}{% endblock title %}
{% block content %}
<h2>News Archive</h2>
<ul>
    {% set recent_changes = get_section(path="news/_index.md") %}
    {% for page in recent_changes.pages %}
    {% if page.date and not page.template %}
    <li>
        {{page.date}} - <a href="{{ page.permalink | safe }}">{{ page.title }}</a>
    </li>
    {% endif %}
    {% endfor %}
</ul>
{% endblock content %}

content/news/archive/_index.md:

+++
title = "News Archive"
template = "archive.html"
+++

You can also do {% for page in recent_changes.pages | slice(end=4) %} to remove the need for the if. end is 0-indexed, hence 4.

1 Like

That’s a much nicer way of doing it, thank you!