Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja2 Group by Month/year

Tags:

jinja2

I'm trying to group a list of date/times in Jinja by month/year. Here's the code I have right now:

{% for group in EventsList|groupby('date') %}
        <b>{{group.grouper}}</b><br />
        {% for event in group.list %}
            <i>{{event.title}}</i>
        {% endfor %}
    {% endfor %}

But the problem is that it currently groups by a specific date. I'd like to group by Month/Year (i.e. January 2011, February 2011 etc..).

Would it be more efficient to do this in Python instead?

thanks!

like image 457
iceanfire Avatar asked Oct 06 '12 22:10

iceanfire


1 Answers

You could first groupby('date.year') and then groupby('date.month').

{% for year, year_group in EventsList|groupby('date.year') %}
    {% for month, list in year_group|groupby('date.month') %}
        <b>{{ month }} {{ year }}</b><br />
        {% for event in list %}
            <i>{{event.title}}</i>
        {% endfor %}
    {% endfor %}
{% endfor %}
like image 64
Nathan Villaescusa Avatar answered Oct 19 '22 09:10

Nathan Villaescusa