Dotiac::DTL::Tag::regroup - The {% regroup LIST by PROPERTY as NEWVARIABLE %} tag
Template file:
{% regroup loop by gender as grouped %} {% for group in grouped %} <h1>{{ group.grouper }}</h1> {% for entry in group.list %} <p>{{ entry }}</p> {% endfor %} {% endfor %}
Regroups a LIST of objects, hashes or list by a common PROPERTY and saves it into a NEW VARIABLE.
The resulting NEW VARIABLE is a list containing hashes with a "grouper" string, containing the text which was grouped by and a "list" which contains all the objects with the same "grouper" string
This is best explained with an example. If you have this datastructure, each a blog post with a category:
Posts=>[ {title=>"I love food",text=>"I really do",category=>"My life"}, {title=>"I love TV",text=>"Even more than food",category=>"My life"}, {title=>"Simpsons",text=>"Awesome TV show",category=>"TV shows"}, {title=>"ANTM",text=>"I love this one",category=>"TV shows"}, {title=>"xkcd",text=>"The best webcomic",category=>"Webcomics"} ]
Now you want to group it by that "category" in the template:
{% regroup Posts by category as posts_grouped %} {% for cat in posts_grouped %} <h1>Category: {{ cat.grouper }}</h1> {% for entry in cat.list %} <h2>{{ entry.title }}</h2> {{ entry.text|linebreaks }} {% endfor %} {% endfor %}
This will result in this rendered template:
<h1>Category: My life</h1> <h2>I love food</h2> <p>I really do</p> <h2>I love TV</h2> <p>Even more than food</p> <h1>Category: TV shows</h1> <h2>Simpsons</h2> <p>Awesome TV show</p> <h2>ANTM</h2> <p>I love this one</p> <h1>Category: Webcomics</h1> <h2>xkcd</h2> <p>The best webcomic</p>
Django has another fine example for this: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup
Django's regroup tag needs the LIST to be sorted, this implementation doesn't need it.
I don't know about Django, but here PROPERY can also contain filters.
{% regroup loop by content|length as grouped %}
http://www.djangoproject.com, Dotiac::DTL
Dotiac::DTL was built according to http://docs.djangoproject.com/en/dev/ref/templates/builtins/.
Marc-Sebastian Lucksch
perl@marc-s.de
To install Dotiac::DTL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dotiac::DTL
CPAN shell
perl -MCPAN -e shell install Dotiac::DTL
For more information on module installation, please visit the detailed CPAN module installation guide.