Intermediate commit - just have to add/fix POD for two classes, then done
[p5sagit/Excel-Template.git] / lib / Excel / Template / Container / Loop.pm
CommitLineData
d0eafc11 1package Excel::Template::Container::Loop;
2
3use strict;
4
5BEGIN {
6 use vars qw(@ISA);
7 @ISA = qw(Excel::Template::Container);
8
9 use Excel::Template::Container;
10}
11
12sub new
13{
14 my $class = shift;
15 my $self = $class->SUPER::new(@_);
16
17 if (exists $self->{MAXITERS} && $self->{MAXITERS} < 1)
18 {
19 die "<loop> MAXITERS must be greater than or equal to 1", $/;
20 }
21 else
22 {
23 $self->{MAXITERS} = 0;
24 }
25
26 return $self;
27}
28
6dd4c89d 29sub _make_iterator
d0eafc11 30{
31 my $self = shift;
32 my ($context) = @_;
33
6dd4c89d 34 return Excel::Template::Factory->_create('ITERATOR',
d0eafc11 35 NAME => $context->get($self, 'NAME'),
36 MAXITERS => $context->get($self, 'MAXITERS'),
37 CONTEXT => $context,
38 );
39}
40
41sub render
42{
43 my $self = shift;
44 my ($context) = @_;
45
46 unless ($self->{ITERATOR} && $self->{ITERATOR}->more_params)
47 {
6dd4c89d 48 $self->{ITERATOR} = $self->_make_iterator($context);
d0eafc11 49 }
50 my $iterator = $self->{ITERATOR};
51
52 $iterator->enter_scope;
53
54 while ($iterator->can_continue)
55 {
56 $iterator->next;
57
58 unless ($self->iterate_over_children($context))
59 {
60 $iterator->back_up;
61 last;
62 }
63 }
64
65 $iterator->exit_scope;
66
67 return 0 if $iterator->more_params;
68
69 return 1;
70}
71
72sub total_of
73{
74 my $self = shift;
75 my ($context, $attr) = @_;
76
6dd4c89d 77 my $iterator = $self->_make_iterator($context);
d0eafc11 78
79 my $total = 0;
80
81 $iterator->enter_scope;
82 while ($iterator->can_continue)
83 {
84 $iterator->next;
85 $total += $self->SUPER::total_of($context, $attr);
86 }
87 $iterator->exit_scope;
88
89 return $total;
90}
91
92sub max_of
93{
94 my $self = shift;
95 my ($context, $attr) = @_;
96
6dd4c89d 97 my $iterator = $self->_make_iterator($context);
d0eafc11 98
99 my $max = $context->get($self, $attr);
100
101 $iterator->enter_scope;
102 while ($iterator->can_continue)
103 {
104 $iterator->next;
105 my $v = $self->SUPER::max_of($context, $attr);
106
107 $max = $v if $max < $v;
108 }
109 $iterator->exit_scope;
110
111 return $max;
112}
113
1141;
115__END__
116
117=head1 NAME
118
119Excel::Template::Container::Loop
120
121=head1 PURPOSE
122
123To provide looping
124
125=head1 NODE NAME
126
127LOOP
128
129=head1 INHERITANCE
130
131Excel::Template::Container
132
133=head1 ATTRIBUTES
134
135=over 4
136
137=item * NAME
138
139This is the name of the loop. It's used to identify within the parameter set
140what variables to expose to the children nodes each iteration.
141
6dd4c89d 142=back
d0eafc11 143
144=head1 CHILDREN
145
146None
147
148=head1 EFFECTS
149
150None
151
152=head1 DEPENDENCIES
153
154None
155
156=head1 USAGE
157
158 <loop name="LOOPY">
159 ... Children here ...
160 </loop>
161
162In the above example, the children nodes would have access to the LOOPY array
163of hashes as parameters. Each iteration through the array would expose a
164different hash of parameters to the children.
165
166These loops work just like HTML::Template's loops. (I promise I'll give more
167info here!)
168
169There is one difference - I prefer using Perl-like scoping, so accessing of
170variables outside the LOOP scope from within is perfectly acceptable. You can
171also hide outside variables with inner values, if you desire, just like Perl.
172
173=head1 AUTHOR
174
c09684ff 175Rob Kinyon (rob.kinyon@gmail.com)
d0eafc11 176
177=head1 SEE ALSO
178
179=cut