I'm iterating through a list with a conditional as follows:
{% for a in articles if a.category == article.category %}
<li{% if a == article %} class="current"{% endif %}><a href="{{ SITEURL }}/{{ a.url }}">{{ a.title }}</a></li>
{% endfor %}
However, I only want to show the 4 most recent articles in the same category as the current article. This is Pelican, so the current article being rendered has a variable name of article
. Hence the conditional. As the code stands, it will list every article in the same category. Good for now, but this will become a problem in the future.
I've tried enclosing the list in parentheses as follows:
{% for a in (articles if a.category == article.category)[:4] %}
I've also tried a slice filter to no avail:
{% for a in articles if a.category == article.category | slice(4) %}
Any help would be most valued. Many thanks.
If you use the selectattr
filter to filter your list, rather than a loop filter expression, you can get what you want:
{% for a in (articles|selectattr('category', 'equalto', article.category)|list)[:4] %}
<li{% if a == article %} class="current"{% endif %}><a href="{{ SITEURL }}/{{ a.url }}">{{ a.title }}</a></li>
{% endfor %}
Given the above template and this sample code:
import jinja2
articles = [
dict(category='foo', title='Article 1 about Foo'),
dict(category='foo', title='Article 2 about Foo'),
dict(category='bar', title='Article 1 about Bar'),
dict(category='baz', title='Article 1 about Baz'),
dict(category='foo', title='Article 3 about Foo'),
dict(category='foo', title='Article 4 about Foo'),
dict(category='foo', title='Article 5 about Foo'),
dict(category='foo', title='Article 6 about Foo'),
]
with open('template.html') as fd:
template = jinja2.Template(fd.read())
print(template.render(articles=articles))
This produces:
<li><a href="/">Article 1 about Foo</a></li>
<li><a href="/">Article 2 about Foo</a></li>
<li><a href="/">Article 3 about Foo</a></li>
<li><a href="/">Article 4 about Foo</a></li>