Marc - Sebastian Lucksch > Dotiac-0.8 > Dotiac::DTL::Tag::regroup

Download:
dotiac/Dotiac-0.8.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
View/Report Bugs
Module Version: 0.8   Source   Latest Release: Dotiac-0.8_1

NAME ^

Dotiac::DTL::Tag::regroup - The {% regroup LIST by PROPERTY as NEWVARIABLE %} tag

SYNOPSIS ^

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 %}

DESCRIPTION ^

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

BUGS AND DIFFERENCES TO DJANGO ^

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 %}

SEE ALSO ^

http://www.djangoproject.com, Dotiac::DTL

LEGAL ^

Dotiac::DTL was built according to http://docs.djangoproject.com/en/dev/ref/templates/builtins/.

AUTHOR ^

Marc-Sebastian Lucksch

perl@marc-s.de

syntax highlighting: