jandecaluwe / urubu

A micro CMS for static websites, with a focus on good navigation practices.
urubu.jandecaluwe.com
GNU Affero General Public License v3.0
179 stars 36 forks source link

Pagination #58

Open dmpour23 opened 7 years ago

dmpour23 commented 7 years ago

Hi is their a way to include pagination i.e top 5 -10 posts /news.

jandecaluwe commented 7 years ago

Yes, by using the features of the Jinja2 templating language on the site data structure that is made available to it automatically.

jshaffstall commented 3 years ago

Can you point me in the right direction for pagination? For example, I have an index.md that gets a list of all the files in the current directory. I'd like that list to be displayed in paginated form, which suggests Urubu generating index.md, index2.md, index3.md, etc, depending on how many items are shown on each page.

Does Urubu do that?

jshaffstall commented 3 years ago

If someone else comes here looking for pagination, I have a fork that does it in the way I needed: https://github.com/jshaffstall/urubu

It may not play well with all of Urubu's features, since I'm not currently using all of them. But it works for my use case. The changes are entirely in processors.py, if you want to update your copy of Urubu in site-packages.

To generate paged index files, add the following front matter:

items_per_page: 5
items_index: this

Change items_per_page to suit your needs. This will generate enough index.html variants (index.html, index2.html, index3.html, etc) to display all the items in chunks of items_per_page.

Each page will get some new attributes added to help with templates.

numpages - the number of total pages generated thispage - the page number of this page in the chain prevpage - the previous page in the chain nextpage - the next page in the chain pages - a list of page numbers and page objects

As an example of template navigation using these, this is what I used for mine:

      {% if this.numpages %}
                        <div class="block-27">
                          <ul>
      {% if this.prevpage %}
                            <li><a href="{{this.prevpage.url}}">&lt;</a></li>
      {% endif %}
      {% for page in this.pages %}
      {% if page.pagenum == this.thispage %}
                            <li class="active"><span>{{page.pagenum}}</span></li>
      {% else %}
                            <li><a href="{{page.page.url}}">{{page.pagenum}}</a></li>
      {% endif %}
      {% endfor %}
      {% if this.nextpage %}
                            <li><a href="{{this.nextpage.url}}">&gt;</a></li>
      {% endif %}
                          </ul>
                        </div>
      {% endif %}

You can also use this for non-index files if you have a suitable filter that will generate the items you want to display. Use this front matter instead:

items_per_page: 5
items_filter: mysteries narrative categories

Where the first word in the items_filter attribute is the name of the filter function, and the remaining words are parameters to that filter function.