如何使用Jinja2在鹈鹕中按日期和分页对文章进行分组? [英] How can I use Jinja2 to group articles by date and paginate in Pelican?

查看:106
本文介绍了如何使用Jinja2在鹈鹕中按日期和分页对文章进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Pelican静态网站生成器来创建大量博客.鹈鹕主题在索引页面上分页显示了帖子标题和摘要列表,并按日期对帖子进行排序.下面是引导主题的示例:

I'm using the Pelican static site generator to create a high-volume blog. Pelican themes paginate the index page, showing a list of post titles and summaries, sorting the posts by date. Here's an example of how this is accomplished, from the bootstrap theme:

{% if articles %}
{% for article in (articles_page.object_list if articles_page else articles) %}
<div class='article'>
<h2>{{ article.title }}</h2>
<div class="well small">{% include "metadata.html" %}</div>
<div class="summary">{{ article.summary }} <a class="btn primary xsmall" href="{{ SITEURL }}/{{ article.url }}">more…</a>
</div>
</div>  
{% endfor %}
{%endif%}

这是分页导航的非常标准的代码:

And here's the also-pretty-standard code for the pagination navigation:

{% if articles_page and articles_paginator.num_pages > 1 %}
<div class="pagination">
<ul>
{% if articles_page.has_previous() %}
    {% set num = articles_page.previous_page_number() %}
    <li class="prev"><a href="{{ SITEURL }}/{{ page_name }}{{ num if num > 1 else '' }}.html">&larr; Previous</a></li>
{% else %}
    <li class="prev disabled"><a href="#">&larr; Previous</a></li>
{% endif %}
{% for num in range( 1, 1 + articles_paginator.num_pages ) %}
    <li class="{{ 'active' if num == articles_page.number else '' }}"><a href="{{ SITEURL }}/{{ page_name }}{{ num if num > 1 else '' }}.html">{{ num }}</a></li>
{% endfor %}
{% if articles_page.has_next() %}
    <li class="next"><a href="{{ SITEURL }}/{{ page_name }}{{ articles_page.next_page_number() }}.html">Next &rarr;</a></li>
{% else %}
    <li class="next disabled"><a href="#">&rarr; Next</a></li>
{% endif %}

由于我的网站有很多信息要在一个很小的空间中共享(有时每天20篇文章),因此我在一行中编写了摘要.我希望索引页面不按每个帖子列出日期,而是按日期对帖子进行分组,如下所示:

Since my site has lots of information to share in a small space--sometimes 20 articles a day--I've written summaries fit in a single line. Instead of listing the date with each post, I'd like the index page to group posts by date, like this:

2014年2月1日
帖子1
帖子2
发布3

February 1, 2014
Post 1
Post 2
Post 3

2014年2月2日
发布1
发布2

February 2, 2014
Post 1
Post 2

这是使用Jinja2按日期对文章进行分组的一种方法:

Here's a way to group articles by date with Jinja2:

{% if articles %}
{% for year, year_articles in articles|groupby('date.year')|sort(reverse=True) %}
{% for month, month_articles in year_articles|groupby('date.month')|sort(reverse=True) %}
{% for day, day_articles in month_articles|groupby('date.day')|sort(reverse=True) %}
<dl>
    <dt>{{ day_articles[0].date.strftime('%d %B %Y') }}</dt>
        {% for article in day_articles %}
        <dd>
        <a href="{{ SITEURL }}/{{ article.url }}" rel="bookmark" title="Permalink to {{ article.title|striptags }}">{{ article.title }}</a>
        </dd>
        {% endfor %}
</dl>
{% endfor %}
{% endfor %}
{% endfor %}
{% endif %}    

我想结合这些功能,以便按日期对文章进行分组并分页.到目前为止,我的笔试失败了.我开始使用100篇文章,设置为每页显示10篇文章;在我的尝试中,索引列出了10页文章,但它显示了每页上的所有文章.我对任何可行的解决方案都很满意.任何想法如何进行?

I want to combine these features so that the articles are grouped by date and paginated. So far my admitted-guesswork has failed. I'm using 100 articles to start with, set to show 10 articles per page; in my attempts, the index lists 10 pages of articles but it shows all of the articles on each page. I'd be happy with any working solution. Any ideas how to proceed?

其他想法
也许不是所有分组,而是一个Jinja if循环可以标识该日期列出的第一篇文章并写上日期,然后是链接的文章标题,等等.对于所有后续文章,它将跳过打印日期并写出链接的文章标题等.我不确定如何做到这一点,并且if循环仍必须避免将分页器从游戏中淘汰掉.但是,如果可行,创建美观列表是CSS作业,而不是Jinja作业.

Further thoughts
Maybe instead of all the grouping, a Jinja if-loop could identify the first article listed for that date and write the date, then the linked article title, etc. For all subsequent articles, it would skip printing the date and write the linked article title, etc. I'm not sure how to do that, and that if-loop would still have to avoid knocking the paginator off its game. But if it works, creating a nice-looking list is a CSS job instead of a Jinja job.

推荐答案

可能为时已晚,无法提供帮助,但我正在分享,因为我花了一个晚上与之战斗,最终使其在Pelican 4.2中工作.我没有按单个日期进行分组,但这仍然可以在一天中使用额外的内部循环进行操作:

Probably far too late to be helpful, but I'm sharing because I just spent an evening fighting with this and finally got it working in Pelican 4.2. I'm not grouping by individual date, but this should still work with an extra inner loop for the day:

{% for year, year_group in dates_page.object_list|groupby('date.year')|reverse %}
    {% for month, month_group in year_group|groupby('date.month')|reverse %}
        <h1>{{ (month_group|first).date|strftime('%B %Y') }}</h1>
        {% for article in month_group %}

            ... do stuff with individual article here ...

        {% endfor %}
    {% endfor %}
{% endfor %}

{% if dates_page.has_other_pages() %}
    {% include 'pagination.html' %}
{% endif %}

关键是要使用 dates_page.object_list 开始外部循环; dates_page 提供按日期排序的分页文章列表,而object_list是可迭代的,可提供模板使用的文章的实际列表.我尝试使用省略的object_list并导致各种错误的其他示例.

The key thing is to the start outer loop with dates_page.object_list; dates_page provides the paged list of articles ordered by date, and object_list is the iterable that provides the actual list of articles used by the template. Other examples I was trying to use omitted object_list and causing various errors.

底部的代码块根据需要加载分页模板.

The block of code at the bottom loads the pagination template as needed.

这篇关于如何使用Jinja2在鹈鹕中按日期和分页对文章进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆