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