Splitting Pelican pages list

Posted on Sat 20 June 2020 in Programming, Web

For another site/blog I write, with Pelican like this blog, I had the necessity to split the list of the site's pages into 2 indipendent lists. This because some pages contains something like the datasheet of a scale model while others are normal pages (links, about and so on)

After looking for a Pelican's theme that do it natively without finding one, I gave up and hacked a solution that seems to work reasonably well. As bonus it is possible, given the necessary modification to the theme, split the list in more than 2 lists.

Without an in-deep explanation of how Pelican work the solution is somewhat simple.
In every page I put a page_order: n directive, then in the theme code that render the list I checked the value and acted as necessary.

{% if pages %}
 <nav>
   <ul class="list">
     <font size="+2"><b>Schede modelli</b></font>
     {% if not PAGES_SORT_ATTRIBUTE -%}
         {% set PAGES_SORT_ATTRIBUTE = 'title' %}
     {%- endif %}
     {% for page in pages|sort(attribute=PAGES_SORT_ATTRIBUTE) %}
     {% if page.page_order == '1' -%}
     <li><a href="{{ SITEURL }}/{{ page.url }}{% if not DISABLE_URL_HASH %}#{{ page.slug }}{% endif %}">{{ page.title }}</a></li>
     {%- endif %}
     {% endfor %}
   </ul>
   <ul class="list">
     <font size="+2"><b>Meta</b></font>
     {% if not PAGES_SORT_ATTRIBUTE -%}
         {% set PAGES_SORT_ATTRIBUTE = 'title' %}
     {%- endif %}
     {% for page in pages|sort(attribute=PAGES_SORT_ATTRIBUTE) %}
     {% if page.page_order == '2' -%}
     <li><a href="{{ SITEURL }}/{{ page.url }}{% if not DISABLE_URL_HASH %}#{{ page.slug }}{% endif %}">{{ page.title }}</a></li>
     {%- endif %}
     {% endfor %}
   </ul>
 </nav>
 {% endif %}

(the code is from the theme I use, Flex)

It is simple to see how it works: since every page has a page_order: n I just needed to duplicate the rendering code for the pages to be able to have 2 lists and in every list I check for a specific value of n and add the list item only if I the page has the correct page_order value.
As you can see, to have more than 2 lists you just need to duplicate the code, one for every list you want, and update the pages.

It is possible also to see that I logically split the pages list from the links list and done some minor adjustement to the theme, but nothing fancy

Probably it is not the correct way to do it, the page_order directive has another use, but since I don't wanted to build a new plugin, I decided that for my objective it was enough.