【发布时间】:2018-10-04 10:09:13
【问题描述】:
我有以下脚本:
<div class="blog-archives">
{% set last_year = 0 %}
{% for article in dates %}
{% set year = article.date.strftime('%Y') %}
{% if last_year != year %}
<h2 id="{{year }}" title="last_year={{last_year}}"><a href="#{{year}}">{{ year }}</a></h2>
{% set last_year = year %}
{% endif %}
{% set next_year = 0 %}
{% if not loop.last %}
{% set next = loop.index0 + 1 %}
{% set next_article = dates[next] %}
{% set next_year = next_article.date.strftime('%Y') %}
{% endif %}
{% if next_year != year %}
<article class="last-entry-of-year">
{% else %}
<article>
{% endif %}
<a href="{{ SITEURL }}/{{ article.url }}">{{ article.title }} {%if article.subtitle %} <small> {{ article.subtitle }} </small> {% endif %} </a>
<time pubdate="pubdate" datetime="{{ article.date.isoformat() }}">{{ article.locale_date }}</time>
</article>
{% endfor %}
</div>
我认为会导致这样的结果:
但实际上我明白了
问题是{% set last_year = year %} 似乎没有被执行——last_year 的值始终为 0。有人知道为什么以及如何解决它吗?
【问题讨论】: