/usr/share/perl5/Template/Plugin/Cycle.pm is in libtemplate-plugin-cycle-perl 1.06-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 | package Template::Plugin::Cycle;
=pod
=head1 NAME
Template::Plugin::Cycle - Cyclically insert into a Template from a sequence of values
=head1 SYNOPSIS
[% USE cycle('row', 'altrow') %]
<table border="1">
<tr class="[% class %]">
<td>First row</td>
</tr>
<tr class="[% class %]">
<td>Second row</td>
</tr>
<tr class="[% class %]">
<td>Third row</td>
</tr>
</table>
###################################################################
# Alternatively, you might want to make it available to all templates
# throughout an entire application.
use Template::Plugin::Cycle;
# Create a Cycle object and set some values
my $Cycle = Template::Plugin::Cycle->new;
$Cycle->init('normalrow', 'alternaterow');
# Bind the Cycle object into the Template
$Template->process( 'tablepage.html', class => $Cycle );
#######################################################
# Later that night in a Template
<table border="1">
<tr class="[% class %]">
<td>First row</td>
</tr>
<tr class="[% class %]">
<td>Second row</td>
</tr>
<tr class="[% class %]">
<td>Third row</td>
</tr>
</table>
[% class.reset %]
<table border="1">
<tr class="[% class %]">
<td>Another first row</td>
</tr>
</table>
#######################################################
# Which of course produces
<table border="1">
<tr class="normalrow">
<td>First row</td>
</tr>
<tr class="alternaterow">
<td>Second row</td>
</tr>
<tr class="normalrow">
<td>Third row</td>
</tr>
</table>
<table border="1">
<tr class="normalrow">
<td>Another first row</td>
</tr>
</table>
=head1 DESCRIPTION
Sometimes, apparently almost exclusively when doing alternating table row
backgrounds, you need to print an alternating, cycling, set of values
into a template.
Template::Plugin::Cycle is a small, simple, and hopefully DWIM solution to
these sorts of tasks.
It can be used either as a normal Template::Plugin, or can be created
directly and passed in as a template argument, so that you can set up
situations where it is implicitly available in every page.
=head1 METHODS
=cut
use 5.005;
use strict;
use Params::Util '_INSTANCE';
use Template::Plugin ();
use overload 'bool' => sub () { 1 },
'""' => 'next';
use vars qw{$VERSION @ISA};
BEGIN {
$VERSION = '1.06';
@ISA = 'Template::Plugin';
}
#####################################################################
# Constructor
=pod
=head2 new [ $Context ] [, @list ]
The C<new> constructor creates and returns a new C<Template::Plugin::Cycle>
object. It can be optionally passed an initial set of values to cycle
through.
When called from within a Template, the new constructor will be passed the
current L<Template::Context> as the first argument. This will be ignored.
By doing this, you can use it both directly, AND from inside a Template.
=cut
sub new {
my $self = bless [ 0, () ], shift;
# Ignore any Template::Context param
shift if _INSTANCE($_[0], 'Template::Context');
$self->init( @_ ) if @_;
$self;
}
=pod
=head2 init @list
If you need to set the values for a new empty object, of change the values
to cycle through for an existing object, they can be passed to the C<init>
method.
The method always returns the C<''> null string, to avoid inserting
anything into the template.
=cut
sub init {
my $self = ref $_[0] ? shift : return undef;
@$self = ( 0, @_ );
'';
}
#####################################################################
# Main Methods
=pod
=head2 elements
The C<elements> method returns the number of items currently set for the
C<Template::Plugin::Cycle> object.
=cut
sub elements {
my $self = ref $_[0] ? shift : return undef;
$#$self;
}
=pod
=head2 list
The C<list> method returns the current list of values for the
C<Template::Plugin::Cycle> object.
This is also the prefered method for getting access to a value at a
particular position within the list of items being cycled to.
[%# Access a variety of things from the list %]
The first item in the Cycle object is [% cycle.list.first %].
The second item in the Cycle object is [% cycle.list.[1] %].
The last item in the Cycle object is [% cycle.list.last %].
=cut
sub list {
my $self = ref $_[0] ? shift : return undef;
$self->elements ? @$self[ 1 .. $#$self ] : ();
}
=pod
=head2 next
The C<next> method returns the next value from the Cycle. If the end of
the list of valuese is reached, it will "cycle" back the first object again.
This method is also the one called when the object is stringified. That is,
when it appears on its own in a template. Thus, you can do something like
the following.
<!-- An example of alternate row classes in a table-->
<table border="1">
<!-- Explicitly access the next class in the cycle -->
<tr class="[% rowclass.next %]">
<td>First row</td>
</tr>
<!-- This has the same effect -->
<tr class="[% rowclass %]">
<td>Second row</td>
</tr>
</table>
=cut
sub next {
my $self = ref $_[0] ? shift : return undef;
return '' unless $#$self;
$self->[0] = 1 if ++$self->[0] > $#$self;
$self->[$self->[0]];
}
=pod
=head2 value
The C<value> method is an analogy for the C<next> method.
=cut
sub value { shift->next(@_) }
=pod
=head2 reset
If a single C<Template::Plugin::Cycle> object is to be used it multiple
places within a template, and it is important that the same value be first
every time, then the C<reset> method can be used.
The C<reset> method resets the Cycle, so that the next value returned will
be the first value in the Cycle object.
=cut
sub reset {
my $self = ref $_[0] ? shift : return undef;
$self->[0] = 0;
'';
}
1;
=pod
=head1 SUPPORT
Bugs should be submitted via the CPAN bug tracker, located at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Template-Plugin-Cycle>
For other issues, or commercial enhancement or support, contact the author..
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
Thank you to Phase N Australia (L<http://phase-n.com/>) for permitting
the open sourcing and release of this distribution as a spin-off from a
commercial project.
=head1 COPYRIGHT
Copyright 2004 - 2008 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|