sor*_*rin 6 python jinja2 pelican
我正在使用鹈鹕jinja2模板,以生成基于类别的导航菜单,我需要一种方法来控制页面的顺序,或者至少是一个技巧,让我选择要列出的第一页.
{% for a in articles %}
{% if a.category == category %}
<li><a href="{{ SITEURL }}/{{ a.slug }}">{{ a.title }}
{% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
如何使一个特定的文章页面成为第一个.他们的赌注是降价格式.
Seb*_*ebi 10
Pelican 3.5将引入对文章和页面排序的内置支持.您可以在pelicanconf.py中定义元数据属性文章和页面的排序.这两个变量是:
ARTICLE_ORDER_BY = 'attribute'
PAGE_ORDER_BY = 'attribute'
Run Code Online (Sandbox Code Playgroud)
为了使其正常工作,您必须确保:
有了这个基础设施,以正确的顺序输出文章应该适用于您的代码而无需修改.
要解决此问题,您可以禁用自动显示的类别和页面,并在配置中手动设置菜单项:
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'),
)
Run Code Online (Sandbox Code Playgroud)
您可以使用Pelican的自定义页面元数据和Jinja2的内置排序过滤器进行排序.
示例模板:
{% for pg in PAGES|sort(attribute='sortorder') %}
<li{% if pg == page %} class="active"{% endif %}><a href="{{ SITEURL }}/{{ pg.url }}">{{ pg.title }}</a></li>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
示例页面元数据:
title: User's Manual
date: 2014-06-11 15:11
sortorder: 20
Run Code Online (Sandbox Code Playgroud)