Improved tests and documentation. Fixed a few bugs in register()
[p5sagit/Excel-Template.git] / lib / Excel / Template / Container / Loop.pm
1 package Excel::Template::Container::Loop;
2
3 use strict;
4
5 BEGIN {
6     use vars qw(@ISA);
7     @ISA = qw(Excel::Template::Container);
8
9     use Excel::Template::Container;
10 }
11
12 sub 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
29 sub _make_iterator
30 {
31     my $self = shift;
32     my ($context) = @_;
33
34     return Excel::Template::Factory->_create('ITERATOR',
35         NAME     => $context->get($self, 'NAME'),
36         MAXITERS => $context->get($self, 'MAXITERS'),
37         CONTEXT  => $context,
38     );
39 }
40
41 sub render
42 {
43     my $self = shift;
44     my ($context) = @_;
45
46     unless ($self->{ITERATOR} && $self->{ITERATOR}->more_params)
47     {
48         $self->{ITERATOR} = $self->_make_iterator($context);
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
72 #sub total_of
73 #{
74 #    my $self = shift;
75 #    my ($context, $attr) = @_;
76 #
77 #    my $iterator = $self->_make_iterator($context);
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 #
92 #sub max_of
93 #{
94 #    my $self = shift;
95 #    my ($context, $attr) = @_;
96 #
97 #    my $iterator = $self->_make_iterator($context);
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
114 1;
115 __END__
116
117 =head1 NAME
118
119 Excel::Template::Container::Loop
120
121 =head1 PURPOSE
122
123 To provide looping
124
125 =head1 NODE NAME
126
127 LOOP
128
129 =head1 INHERITANCE
130
131 Excel::Template::Container
132
133 =head1 ATTRIBUTES
134
135 =over 4
136
137 =item * NAME
138
139 This is the name of the loop. It's used to identify within the parameter set
140 what variables to expose to the children nodes each iteration.
141
142 =back
143
144 =head1 CHILDREN
145
146 None
147
148 =head1 EFFECTS
149
150 None
151
152 =head1 DEPENDENCIES
153
154 None
155
156 =head1 USAGE
157
158   <loop name="LOOPY">
159     ... Children here ...
160   </loop>
161
162 In the above example, the children nodes would have access to the LOOPY array
163 of hashes as parameters. Each iteration through the array would expose a
164 different hash of parameters to the children.
165
166 These loops work just like HTML::Template's loops. (I promise I'll give more
167 info here!)
168
169 There is one difference - I prefer using Perl-like scoping, so accessing of
170 variables outside the LOOP scope from within is perfectly acceptable. You can
171 also hide outside variables with inner values, if you desire, just like Perl.
172
173 =head1 AUTHOR
174
175 Rob Kinyon (rob.kinyon@gmail.com)
176
177 =head1 SEE ALSO
178
179 =cut