1 #============================================================= -*-Perl-*-
3 # Template::Plugin::Table
6 # Plugin to order a linear data set into a virtual 2-dimensional table
7 # from which row and column permutations can be fetched.
10 # Andy Wardley <abw@wardley.org>
13 # Copyright (C) 2000-2007 Andy Wardley. All Rights Reserved.
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
18 #============================================================================
20 package Template::Plugin::Table;
24 use base 'Template::Plugin';
25 use Scalar::Util 'blessed';
31 #------------------------------------------------------------------------
32 # new($context, \@data, \%args)
34 # This constructor method initialises the object to iterate through
35 # the data set passed by reference to a list as the first parameter.
36 # It calculates the shape of the permutation table based on the ROWS
37 # or COLS parameters specified in the $args hash reference. The
38 # OVERLAP parameter may be provided to specify the number of common
39 # items that should be shared between subseqent columns.
40 #------------------------------------------------------------------------
43 my ($class, $context, $data, $params) = @_;
44 my ($size, $rows, $cols, $coloff, $overlap, $error);
46 # if the data item is a reference to a Template::Iterator object,
47 # or subclass thereof, we call its get_all() method to extract all
48 # the data it contains
49 if (blessed($data) && $data->isa('Template::Iterator')) {
50 ($data, $error) = $data->get_all();
51 return $class->error("iterator failed to provide data for table: ",
56 return $class->error('invalid table data, expecting a list')
57 unless ref $data eq 'ARRAY';
60 return $class->error('invalid table parameters, expecting a hash')
61 unless ref $params eq 'HASH';
63 # ensure keys are folded to upper case
64 @$params{ map { uc } keys %$params } = values %$params;
66 $size = scalar @$data;
67 $overlap = $params->{ OVERLAP } || 0;
69 # calculate number of columns based on a specified number of rows
70 if ($rows = $params->{ ROWS }) {
77 $coloff = $rows - $overlap;
78 $cols = int ($size / $coloff)
79 + ($size % $coloff > $overlap ? 1 : 0)
82 # calculate number of rows based on a specified number of columns
83 elsif ($cols = $params->{ COLS }) {
90 $coloff = int ($size / $cols)
91 + ($size % $cols > $overlap ? 1 : 0);
92 $rows = $coloff + $overlap;
107 _OVERLAP => $overlap,
108 _PAD => defined $params->{ PAD } ? $params->{ PAD } : 1,
113 #------------------------------------------------------------------------
116 # Returns a reference to a list containing the items in the row whose
117 # number is specified by parameter. If the row number is undefined,
118 # it calls rows() to return a list of all rows.
119 #------------------------------------------------------------------------
122 my ($self, $row) = @_;
123 my ($data, $cols, $offset, $size, $pad)
124 = @$self{ qw( _DATA _NCOLS _COLOFF _SIZE _PAD) };
127 # return all rows if row number not specified
131 return () if $row >= $self->{ _NROWS } || $row < 0;
135 for (my $c = 0; $c < $cols; $c++) {
136 push(@set, $index < $size
138 : ($pad ? undef : ()));
145 #------------------------------------------------------------------------
148 # Returns a reference to a list containing the items in the column whose
149 # number is specified by parameter. If the column number is undefined,
150 # it calls cols() to return a list of all columns.
151 #------------------------------------------------------------------------
154 my ($self, $col) = @_;
155 my ($data, $size) = @$self{ qw( _DATA _SIZE ) };
159 # return all cols if row number not specified
163 return () if $col >= $self->{ _NCOLS } || $col < 0;
165 $start = $self->{ _COLOFF } * $col;
166 $end = $start + $self->{ _NROWS } - 1;
167 $end = $start if $end < $start;
169 $blanks = ($end - $size) + 1;
172 return () if $start >= $size;
173 return [ @$data[$start..$end],
174 $self->{ _PAD } ? ((undef) x $blanks) : () ];
178 #------------------------------------------------------------------------
181 # Returns all rows as a reference to a list of rows.
182 #------------------------------------------------------------------------
186 return [ map { $self->row($_) } (0..$self->{ _NROWS }-1) ];
190 #------------------------------------------------------------------------
193 # Returns all rows as a reference to a list of rows.
194 #------------------------------------------------------------------------
198 return [ map { $self->col($_) } (0..$self->{ _NCOLS }-1) ];
202 #------------------------------------------------------------------------
205 # Provides read access to various internal data members.
206 #------------------------------------------------------------------------
210 my $item = $AUTOLOAD;
212 return if $item eq 'DESTROY';
214 if ($item =~ /^(?:data|size|nrows|ncols|overlap|pad)$/) {
215 return $self->{ $item };
218 return (undef, "no such table method: $item");
230 Template::Plugin::Table - Plugin to present data in a table
234 [% USE table(list, rows=n, cols=n, overlap=n, pad=0) %]
236 [% FOREACH item IN table.row(n) %]
240 [% FOREACH item IN table.col(n) %]
244 [% FOREACH row IN table.rows %]
245 [% FOREACH item IN row %]
250 [% FOREACH col IN table.cols %]
251 [% col.first %] - [% col.last %] ([% col.size %] entries)
256 The C<Table> plugin allows you to format a list of data items into a
257 virtual table. When you create a C<Table> plugin via the C<USE> directive,
258 simply pass a list reference as the first parameter and then specify
259 a fixed number of rows or columns.
261 [% USE Table(list, rows=5) %]
262 [% USE table(list, cols=5) %]
264 The C<Table> plugin name can also be specified in lower case as shown
265 in the second example above. You can also specify an alternative variable
266 name for the plugin as per regular Template Toolkit syntax.
268 [% USE mydata = table(list, rows=5) %]
270 The plugin then presents a table based view on the data set. The data
271 isn't actually reorganised in any way but is available via the C<row()>,
272 C<col()>, C<rows()> and C<cols()> as if formatted into a simple two dimensional
273 table of C<n> rows x C<n> columns.
275 So if we had a sample C<alphabet> list contained the letters 'C<a>' to 'C<z>',
276 the above C<USE> directives would create plugins that represented the following
277 views of the alphabet.
279 [% USE table(alphabet, ... %]
282 a f k p u z a g m s y
289 We can request a particular row or column using the C<row()> and C<col()>
292 [% USE table(alphabet, rows=5) %]
293 [% FOREACH item = table.row(0) %]
294 # [% item %] set to each of [ a f k p u z ] in turn
297 [% FOREACH item = table.col(2) %]
298 # [% item %] set to each of [ m n o p q r ] in turn
301 Data in rows is returned from left to right, columns from top to
302 bottom. The first row/column is 0. By default, rows or columns that
303 contain empty values will be padded with the undefined value to fill
304 it to the same size as all other rows or columns.
306 For example, the last row (row 4) in the first example would contain the
307 values C<[ e j o t y undef ]>. The Template Toolkit will safely accept these
308 undefined values and print a empty string. You can also use the IF directive
309 to test if the value is set.
311 [% FOREACH item = table.row(4) %]
317 You can explicitly disable the C<pad> option when creating the plugin to
318 returned shortened rows/columns where the data is empty.
320 [% USE table(alphabet, cols=5, pad=0) %]
321 [% FOREACH item = table.col(4) %]
322 # [% item %] set to each of 'y z'
325 The C<rows()> method returns all rows/columns in the table as a reference
326 to a list of rows (themselves list references). The C<row()> methods
327 when called without any arguments calls C<rows()> to return all rows in
330 Ditto for C<cols()> and C<col()>.
332 [% USE table(alphabet, cols=5) %]
333 [% FOREACH row = table.rows %]
334 [% FOREACH item = row %]
339 The Template Toolkit provides the C<first>, C<last> and C<size> virtual
340 methods that can be called on list references to return the first/last entry
341 or the number of entries in a list. The following example shows how we might
342 use this to provide an alphabetical index split into 3 even parts.
344 [% USE table(alphabet, cols=3, pad=0) %]
345 [% FOREACH group = table.col %]
346 [ [% group.first %] - [% group.last %] ([% group.size %] letters) ]
349 This produces the following output:
351 [ a - i (9 letters) ]
352 [ j - r (9 letters) ]
353 [ s - z (8 letters) ]
355 We can also use the general purpose C<join> virtual method which joins
356 the items of the list using the connecting string specified.
358 [% USE table(alphabet, cols=5) %]
359 [% FOREACH row = table.rows %]
360 [% row.join(' - ') %]
363 Data in the table is ordered downwards rather than across but can easily
364 be transformed on output. For example, to format our data in 5 columns
365 with data ordered across rather than down, we specify C<rows=5> to order
374 and then iterate down through each column (a-e, f-j, etc.) printing
382 Example code to do so would be much like the following:
384 [% USE table(alphabet, rows=3) %]
385 [% FOREACH cols = table.cols %]
386 [% FOREACH item = cols %]
399 In addition to a list reference, the C<Table> plugin constructor may be passed
400 a reference to a L<Template::Iterator> object or subclass thereof. The
401 L<Template::Iterator> L<get_all()|Template::Iterator#get_all()> method is
402 first called on the iterator to return all remaining items. These are then
403 available via the usual Table interface.
405 [% USE DBI(dsn,user,pass) -%]
407 # query() returns an iterator
408 [% results = DBI.query('SELECT * FROM alphabet ORDER BY letter') %]
410 # pass into Table plugin
411 [% USE table(results, rows=8 overlap=1 pad=0) -%]
413 [% FOREACH row = table.cols -%]
414 [% row.first.letter %] - [% row.last.letter %]:
420 Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>
424 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
426 This module is free software; you can redistribute it and/or
427 modify it under the same terms as Perl itself.
437 # perl-indent-level: 4
438 # indent-tabs-mode: nil
441 # vim: expandtab shiftwidth=4: