2

我正在使用 Pelican 静态站点生成器来创建高容量博客。Pelican 主题对索引页面进行分页,显示帖子标题和摘要列表,按日期对帖子进行排序。这是一个如何完成的示例,来自bootstrap 主题

{% 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…&lt;/a>
</div>
</div>  
{% endfor %}
{%endif%}

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

{% 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 篇文章——我已经写了适合一行的摘要。我希望索引页面按日期对帖子进行分组,而不是列出每个帖子的日期,如下所示:

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

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

这是一种使用 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 页文章,但它显示了每页上的所有文章。我会对任何可行的解决方案感到满意。任何想法如何进行?

进一步的思考
也许 Jinja if 循环可以识别为该日期列出的第一篇文章并写下日期,然后是链接的文章标题等,而不是所有分组。对于所有后续文章,它将跳过打印日期并写下链接的文章标题等。我不知道该怎么做,而且 if-loop 仍然必须避免将分页器从游戏中剔除。但如果可行,创建一个漂亮的列表是一项 CSS 工作,而不是 Jinja 工作。

4

2 回答 2

1

使用dates_page变量而不是文章,在此处查看详细信息:http: //pelican.readthedocs.org/en/latest/themes.html#index-html

  • dates_paginator 文章列表的分页器对象,按日期排序,升序。
  • dates_page 当前页面的文章,按日期排序,升序。
于 2014-02-24T23:25:21.157 回答
1

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 %}

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.

于 2020-02-14T04:07:08.433 回答