How to count authors in Pelican Blogs? - python

I was trying to make my blog show "Authors" instead of "Author" when there are more than 1 authors for the same article.
The solution (thanks to #Avaris) is to set i = article.authors|count and that can be used as follows
<div align="right">
{% set i = article.authors|count %}
{% if i != 1 %}
Authors:<br>
{% endif %}
{% if i == 1 %}
Author:<br>
{% endif %}
{% if article.authors %}
{% for author in article.authors %}
{{ author }}<br>
{% endfor %}
{% endif %}
</div>
This is different to Set variable in jinja because in Pelican I cannot find a command like lenght(authors) to obtain a number to define a condition.

Related

Django Template For Loop - reverese last two iterations

I am using Django 1.10 and Python 3.5.
I have been given some code that consists of a for loop that returns 3 iterations of value in alphabetical order.
For example the for loop returns: Chronological, Combination, Functional.
However I need the for loop to return: Chronological, Functional, Combination (the last two iterations/values are reversed). I am forced to keep the for loop.
Is this at all possible with a for loop?
I have tried combining {% if forloop.last %} with {% if forloop.first%} and also setting the value with {% if forloop.counter == 2 %}...set value to Functional...{% endif %} and {% if forloop.counter == 3 %}...set value to Combination...{% endif %} but I cannot get this to achieve what I want.
Here is my code:
{% for resume_format_image in resume_format_images %}
<div class="col-md-4">
{% with "resumes/resume_format_description_"|add:resume_format_image.style.lower|add:".html" as template_name %}
{% include template_name %}
{% endwith %}
</div>
{% endfor %}
I just figured this out:
{% for resume_format_image in resume_format_images %}
<div class="col-md-4">
{% if forloop.first%}
{% include "resumes/resume_format_description_chronological.html" %}
{% elif forloop.last%}
{% include "resumes/resume_format_description_combination.html" %}
{% else %}
{% include "resumes/resume_format_description_functional.html" %}
{% endif %}
</div>
{% endfor %}
I hope this helps someone.

Django {% blocktrans %}: How to handle pluralization inside a for loop?

I have the following loop in my Django template:
{% for item in state.list %}
<div> HTML (CUSTOMERS BY STATE) </div>
<!-- print sum of customers at bottom of list -->
{% if forloop.last %}
<h4>{{ forloop.counter }} Valued Customers</h4>
{% endif %}
{% endfor %}
Obviously, if I end up with only one customer, I'd like to print singular "Valued Customer"
According to Django's docs, one should use blocktrans. Tried the following, a few flavors of nesting:
{% blocktrans count %}
{% if forloop.last %}
<h4>
{{ forloop.counter }}
Valued Customer
{% plural %}
Valued Customers
</h4>
{% endif %}
{% endblocktrans %}
Keep getting TemplateSyntaxError: Invalid block tag: 'blocktrans', expected 'empty' or 'endfor'
Is there no way to combine with another loop? Any ideas how to solve? Thanks!
Here is the working code, thanks to alko:
{% load i18n %}
<!-- ... -->
{% if forloop.last %}
<h4>
{{ forloop.counter }}
{% blocktrans count count=forloop.counter %}
Valued Customer
{% plural %}
Valued Customers
{% endblocktrans %}
</h4>
{% endif %}
Probably, you forgot to load translation tags. Add following line at the top of your template:
{% load i18n %}
After you fix that, note that for a blocktrans tag after count a variable, whose value will serve for plural detection, should be specified, so you probably need something like
{% blocktrans count count=forloop.counter %}
To pluralize use this:
Customer{{ forloop.counter|pluralize }}

Django if statement not working

{% extends "base.html" %}
{% block content %}
<h1>{{ page }}</h1>
{% for category in categories %}
{% if category.page == page %}
<h2>{{ category.title }}!</h2>
{% for item in categoryitems %}
{{ category.title }} {{ item.category }}
{% if item.category == category.title %}
<h3>{{ item.title }}</h3>
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endblock %}
The first statement if category.page == page is working fine, but the other one - if item.category == category.title - doesn't, for the sake of checking if everything is fine I've also added those {{category.title}} and {{item.category}} outputs and they are identical, so why doesn't the loop show me my item.title ? Without the if, it works, but, of course, shows every element in the list, which is smthn I don't need.
Do not depend on the page output to tell you what is correct, since the output is dependent on the code. Instead, do it correctly in the first place.
{% if item.category == category %}

Querying for specific articles (via tag/category) in Pelican themes

Is it possible to set query parameters via jinja in Pelican template files?
index.html
{% if articles %}
{% for article in articles_page.object_list if article.category == 'article' %}
#stuff
{% endfor %}
{% endif %}
This will return articles in the article category, but only if they happen to be in the articles already queried for. The desirable setup would be to grab x articles in y category (or with y tag) - is that possible?
This code snippet works for me to bring back a list of all articles matching a tag:
{% block content %}
<ul>
{% for article in articles if FAVORITES_TAG in article.tags %}
{% if loop.index <= FAVORITES_COUNT %}
<li>{{ article.title }}</li>
{% endif %}
{% endfor %}
</ul>
{% endblock %}
I set the FAVORITES_TAG and FAVORITES_COUNT variables in the config. I hope that helps.
I ran into the same problem, and found a solution
{% if articles %}
{% for article in articles_page.object_list if article.category.name == 'article' %}
#stuff
{% endfor %}
{% endif %}

Listing specific category first in Python

I have a list of categories from DB as following and it works fine + sorted by ID.
{% for category in menu_categories|sort(attribute="id"): %}
<div>
{{ category.name }}
</div>
{% endfor %}
I just need one exception if category='Pizza' exist to list it first.
Unless I misunderstood you, this should do it:
{% for category in menu_categories|sort(attribute="id"): %}
{% if category.name == 'Pizza': %}
<div> {{ category.name }} </div>
{% endif %}
{% endfor %}
{% for category in menu_categories|sort(attribute="id"): %}
{% if category.name != 'Pizza': %}
<div> {{ category.name }} </div>
{% endif %}
{% endfor %}

Categories

Resources