How can I control the order of pages from the Pelican article category?

I am using pelican jinja2 templates to create a category-based navigation menu, and I need a way to control the order of the pages, or at least the trick, so that I can select the first page to be shown.

{% for a in articles %} {% if a.category == category %} <li><a href="{{ SITEURL }}/{{ a.slug }}">{{ a.title }} {% endif %} {% endfor %} 

How to make the first page of the article first. Their source is in markdown format.

+4
source share
4 answers

You can sort using Pelican custom page metadata and the built-in sort Jinja2 filter.

Template example:

 {% for pg in PAGES|sort(attribute='sortorder') %} <li{% if pg == page %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ pg.url }}">{{ pg.title }}</a></li> {% endfor %} 

Page metadata examples:

 title: User Manual date: 2014-06-11 15:11 sortorder: 20 
+6
source

Pelican 3.5 will introduce built-in support for organizing articles and pages. You can define in your file pelicanconf.py with which to sort articles for metadata attributes and pages. Two variables:

 ARTICLE_ORDER_BY = 'attribute' PAGE_ORDER_BY = 'attribute' 

For this to work correctly, you must ensure that:

  • each page / article defines the specified attribute, otherwise you will get a compiler error indicating that the page / article class does not have this attribute
  • your attribute always uses the same number of characters; therefore the values โ€‹โ€‹1, 10 and 100 will not produce the correct order, but 001, 010 and 100 will do

Given this infrastructure, the output of articles in the correct order should work for your code unchanged.

+6
source

To work around this problem, you can automatically turn off the display of categories and pages and manually configure the menu items in the configuration:

 DISPLAY_CATEGORIES_ON_MENU = False DISPLAY_PAGES_ON_MENU = False MENUITEMS = ( ('Home', '/'), ('Archives', '/archives.html'), ('Tags', '/tags.html'), ('Category1', 'category/category1.html'), ('Category2', 'category/category2.html'), ) 
+5
source

It seems that at this moment it is not yet possible. There is a feature request and an outdated patch at https://github.com/getpelican/pelican/issues/420

I will update the answer as soon as it is integrated.

0
source

Source: https://habr.com/ru/post/1499691/


All Articles