\Pluf_Template_Tag_Regroup

Template tag <code>regroup</code>.

Regroup a list of alike objects by a common attribute.

This complex tag is best illustrated by use of an example: say that people is a list of people represented by arrays with first_name, last_name, and gender keys:

$people = array( array('first_name' => 'George', 'last_name' => 'Bush', 'gender' => 'Male'), array('first_name' => 'Bill', 'last_name' => 'Clinton', 'gender' => 'Male'), array('first_name' => 'Margaret', 'last_name' => 'Thatcher', 'gender' => 'Female'), array('first_name' => 'Condoleezza', 'last_name' => 'Rice', 'gender' => 'Female'), array('first_name' => 'Pat', 'last_name' => 'Smith', 'gender' => 'Unknow'), );

...and you'd like to display a hierarchical list that is ordered by gender, like this:

  • Male:
    • George Bush
    • Bill Clinton
  • Female:
    • Margaret Thatcher
    • Condoleezza Rice
  • Unknown:
    • Pat Smith

You can use the {regroup} tag to group the list of people by gender. The following snippet of template code would accomplish this:

{regroup $people, 'gender', 'gender_list'}

    {foreach $gender_list as $gender}
  • {$gender.grouper}:
      {foreach $gender.list as $item}
    • {$item.first_name} {$item.last_name}
    • {/foreach}
  • {/foreach}

Let's walk through this example. {regroup} takes three arguments: the object (array or instance of Pluf_Model or any object) you want to regroup, the attribute to group by,and the name of the resulting object. Here, we're regrouping the people list by the gender attribute and calling the result gender_list. The result is assigned in a context varible of the same name $gender_list.

{regroup} produces a instance of ArrayObject (in this case, $gender_list) of group objects. Each group object has two attributes:

  • grouper -- the item that was grouped by (e.g., the string "Male" or "Female").
  • list -- an ArrayObject of all items in this group (e.g., an ArrayObject of all people with gender='Male').

Note that {regroup} does not order its input!

Based on concepts from the Django regroup template tag.

Summary

Methods
Properties
Constants
__construct()
start()
No public properties found
No constants found
No protected methods found
$context
N/A
No private methods found
No private properties found
N/A

Properties

$context

$context : array

Type

array

Methods

__construct()

__construct(  $context = null) 

Constructor.

Parameters

$context

start()

start(mixed  $data, string  $by, string  $assign) 

Parameters

mixed $data

The object to group.

string $by

The attribute ti group by.

string $assign

The name of the resulting object.

Throws

\InvalidArgumentException

If no argument is provided.