Template::Plugin::Group - Template plugin to group lists into simple subgroups


Template-Plugin-Group documentation Contained in the Template-Plugin-Group distribution.

Index


Code Index:

NAME

Top

Template::Plugin::Group - Template plugin to group lists into simple subgroups

SYNOPSIS

Top

  # In your Template
  [% USE rows = Group(cells, 3) %]

  <table>
  [% FOREACH row IN rows %]
    <tr>
    [% FOREACH cell IN rows %]
      <td class="[% cell.class %]">[% cell.content %]</td>
    [% END %]
    </tr>  
  [% END %]
  <table>

DESCRIPTION

Top

Template::Plugin::Group is a fairly simple (for now) module for grouping a list of things into a number of subgroups.

In this intial implementation you can only group ARRAY references, and they can only be grouped into groups of a numbered size.

In practical terms, you can make columns of things and you can break up a list into smaller chunks (for example to chop a large lists into a number of smaller lists for display purposes)

METHODS

Top

new [ $Context, ] \@ARRAY, $cols [, 'pad' ]

Although this is the "new" method, it doesn't really actually create any objects. It simply takes an array reference, splits up the list into groups, and returns the whole things as another array reference.

The rest you do normally, with normal Template Toolkit commands.

If there isn't a perfectly divisible number of elements normally the last group will have less elements than the rest of the groups. If you provide the optional parameter 'pad', the last group will be padded with additional undef values so that it has the full number.

TO DO

Top

- Support grouping HASH references

- If everything in the list is an object, support group/sort by method

- Support complex multi-level grouping (I have code for this already, but it needs to be rewritten and should probably be a separate plugin).

SUPPORT

Top

Bugs should be submitted via the CPAN bug tracker, located at

http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Template-Plugin-Group

For other issues, or commercial enhancement or support, contact the author.

AUTHOR

Top

Adam Kennedy <cpan@ali.as>

COPYRIGHT

Top


Template-Plugin-Group documentation Contained in the Template-Plugin-Group distribution.
package Template::Plugin::Group;

use 5.005;
use strict;
use Template::Plugin ();
use Params::Util     qw{ _ARRAY _HASH _INSTANCE };

use vars qw{$VERSION @ISA};
BEGIN {
	$VERSION = '1.03';
	@ISA     = 'Template::Plugin';
}





#####################################################################
# Constructor

sub new {
	my $class = shift;
	shift if _INSTANCE($_[0], 'Template::Context');
	unless ( defined $_[1] and $_[1] =~ /^[1-9]\d*$/ ) {
		$class->error('Group constructor argument not a positive integer');
	}
	return $class->_new_array(@_) if _ARRAY($_[0]);
	return $class->_new_hash(@_)  if _HASH($_[0]);
	$class->error('Group constructor argument not an ARRAY or HASH ref');
}

sub _new_array {
	# Make sure to copy the original array in case they care about it
	my ($class, $array_ref, $cols) = @_;
	my @array = @$array_ref;

	# Support the padding option
	if ( grep { defined $_ and lc $_ eq 'pad' } @_ ) {
		my $items = scalar(@array) % $cols;
		push @array, (undef) x $items;
	}

	# Create the outside array and pack it
	my @groups = ();
	while ( @array ) {
		push @groups, [ splice @array, 0, $cols ];
	}

	\@groups;
}

sub _new_hash {
	my ($class, $hash, $cols) = @_;
	$class->error('HASH grouping is not implented in this release');

	# Implementation steps.
	# 1. Get the list of keys, sorted in default order
	# 2. Take groups of these and build new hashs for only those
	#    keys, with the same values as the original.
	# 3. Wrap them all inside an ARRAY ref and return.

	# I'm not sure we can do padding in this case...
}

1;