我使用Pelican靜態網站生成器來創建大量博客。 Pelican主題爲索引頁面分頁,顯示帖子標題和摘要列表,按日期對帖子進行排序。下面是如何實現這一點的例子,從bootstrap theme:如何使用Jinja2按日期分組文章和Pelican中的分頁?
{% 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%}
而這裏的分頁導航的同時,非常標準的代碼:
{% 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">← Previous</a></li>
{% else %}
<li class="prev disabled"><a href="#">← 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 →</a></li>
{% else %}
<li class="next disabled"><a href="#">→ Next</a></li>
{% endif %}
因爲我的網站有很多的信息共享在一個小空間 - 每天有20篇文章 - 我寫的摘要符合一行。而不是列出每個職位的日期,我想索引頁按日期分門別類,就像這樣:
2014年2月1日
後1
後2
後3
2014年2月2日
後1
後2
這裏有一個方法來通過DAT組文章e與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頁的文章,但它顯示了每個頁面上的所有文章。我會很高興與任何工作解決方案。任何想法如何繼續?
進一步的想法
也許不是所有的分組,一個神社,如果環能確定該日期列出的第一篇文章,並寫上日期,然後鏈接的文章標題,等等。對於所有的後續文章中,它會跳過打印日期並寫出鏈接的文章標題等。我不知道該怎麼做,並且if-loop仍然必須避免將分頁符從它的遊戲中刪除。但是,如果它工作,創建一個漂亮的列表是一個CSS作業,而不是Jinja作業。