1 package Excel::Template::Container::Conditional;
3 #GGG Convert <conditional> to be a special case of <switch>?
9 @ISA = qw(Excel::Template::Container);
11 use Excel::Template::Container;
16 (map { $_ => $_ } ( '>', '<', '==', '!=', '>=', '<=' )),
17 (map { $_ => $_ } ( 'gt', 'lt', 'eq', 'ne', 'ge', 'le' )),
20 sub _conditional_passes
25 my $name = $context->get($self, 'NAME');
26 return 0 unless $name =~ /\S/;
28 my $val = $context->param($name);
29 $val = @{$val} while ref $val eq 'ARRAY';
30 $val = ${$val} while ref $val eq 'SCALAR';
32 my $value = $context->get($self, 'VALUE');
35 my $op = $context->get($self, 'OP');
36 $op = defined $op && exists $isOp{$op}
43 /^>$/ && do { $res = ($val > $value); last };
44 /^<$/ && do { $res = ($val < $value); last };
45 /^==$/ && do { $res = ($val == $value); last };
46 /^!=$/ && do { $res = ($val != $value); last };
47 /^>=$/ && do { $res = ($val >= $value); last };
48 /^<=$/ && do { $res = ($val <= $value); last };
49 /^gt$/ && do { $res = ($val gt $value); last };
50 /^lt$/ && do { $res = ($val lt $value); last };
51 /^eq$/ && do { $res = ($val eq $value); last };
52 /^ne$/ && do { $res = ($val ne $value); last };
53 /^ge$/ && do { $res = ($val ge $value); last };
54 /^le$/ && do { $res = ($val le $value); last };
56 die "Unknown operator '$op' in conditional resolve", $/;
62 my $istrue = $val && 1;
64 my $is = uc($context->get($self, 'IS') || 'TRUE');
67 return 0 unless $istrue;
71 warn "Conditional 'is' value was [$is], defaulting to 'FALSE'" . $/
72 if $is ne 'FALSE' && $^W;
85 return 1 unless $self->_conditional_passes($context);
87 return $self->iterate_over_children($context);
93 # my ($context, $attr) = @_;
95 # return 0 unless $self->_conditional_passes($context);
97 # return $self->SUPER::max_of($context, $attr);
103 # my ($context, $attr) = @_;
105 # return 0 unless $self->_conditional_passes($context);
107 # return $self->SUPER::total_of($context, $attr);
115 Excel::Template::Container::Conditional - Excel::Template::Container::Conditional
119 To provide conditional execution of children nodes
127 L<CONTAINER|Excel::Template::Container>
135 This is the name of the parameter to test. It is resolved like any other parameter name. (q.v. L<VAR|Excel::Template::Element::Var> for more info.)
139 If VALUE is set, then a comparison operation is done. The value of NAME is compared to VALUE using the value of OP.
143 If VALUE is set, then this is checked. If it isn't present, then '==' (numeric equality) is assumed. OP must be one of Perl the numeric comparison operators or the string comparison operators. All 6 of each kind is supported.
145 B<Note>: If you want to use < or <=, you must instead use < or <=. This is to make sure it will parse with L<XML::Parser>. You should not need to use > or >= instead of > and >=, respectively.
149 If VALUE is not set, then IS is checked. IS is allowed to be either "TRUE" or "FALSE". The boolean value of NAME is checked against IS.
167 <if name="__ODD__" is="false">
171 In the above example, the children will be executed if the value of __ODD__ (which is set by the L<LOOP|Excel::Template::Container::Loop> node) is false. So, for all even iterations.
175 Rob Kinyon (rob.kinyon@gmail.com)
179 L<LOOP|Excel::Template::Container::Loop>, L<VAR|Excel::Template::Element::Var>