| 1 | package Template::Flute::Container; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Template::Flute::Container - Container object for Template::Flute templates. | ||
| 9 | |||
| 10 | =head1 CONSTRUCTOR | ||
| 11 | |||
| 12 | =head2 new | ||
| 13 | |||
| 14 | Creates Template::Flute::Container object. | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | use base 'Template::Flute'; | ||
| 19 | use Template::Flute::Expression; | ||
| 20 | |||
| 21 | our %expression_cache; | ||
| 22 | |||
| 23 | # Constructor | ||
| 24 | sub new { | ||
| 25 | my ($class, $sob, $spec, $name) = @_; | ||
| 26 | my ($self); | ||
| 27 | |||
| 28 | $self = {sob => $sob}; | ||
| 29 | |||
| 30 | bless $self, $class; | ||
| 31 | |||
| 32 | return $self; | ||
| 33 | } | ||
| 34 | |||
| 35 | =head1 METHODS | ||
| 36 | |||
| 37 | =head2 name | ||
| 38 | |||
| 39 | Returns name of the container. | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | sub name { | ||
| 44 | my ($self) = @_; | ||
| 45 | |||
| 46 | return $self->{sob}->{name}; | ||
| 47 | } | ||
| 48 | |||
| 49 | =head2 list | ||
| 50 | |||
| 51 | Name of list this container belongs to or undef for top level containers. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub list { | ||
| 56 | return shift->{sob}->{list}; | ||
| 57 | } | ||
| 58 | |||
| 59 | =head2 set_values | ||
| 60 | |||
| 61 | Passes current values to this container. | ||
| 62 | |||
| 63 | =cut | ||
| 64 | |||
| 65 | sub set_values { | ||
| 66 | my ($self, $values) = @_; | ||
| 67 | |||
| 68 | $self->{values} = $values; | ||
| 69 | } | ||
| 70 | |||
| 71 | =head2 elts | ||
| 72 | |||
| 73 | Returns corresponding HTML template elements for this container. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub elts { | ||
| 78 | my ($self) = @_; | ||
| 79 | |||
| 80 | return $self->{sob}->{elts}; | ||
| 81 | } | ||
| 82 | |||
| 83 | =head2 visible | ||
| 84 | |||
| 85 | Determines whether the container is visible. Possible return values are 1 (visible), | ||
| 86 | 0 (hidden) or undef if the specification for the container misses a value attribute. | ||
| 87 | |||
| 88 | =cut | ||
| 89 | |||
| 90 | # visible | ||
| 91 | sub visible { | ||
| 92 | my ($self) = @_; | ||
| 93 | my ($key, $ret); | ||
| 94 | |||
| 95 | if ($key = $self->{sob}->{value}) { | ||
| 96 | # check whether this is an expression or a simple value | ||
| 97 | if ($key =~ /^\w[0-9\w_-]*$/) { | ||
| 98 | # value holds method | ||
| 99 | return $self->{values}->$key | ||
| 100 | if $self->_is_record_object($self->{values}) && $self->{values}->can($key); | ||
| 101 | if (exists $self->{values}) { | ||
| 102 | if ($self->{values}->{$key}) { | ||
| 103 | return 1; | ||
| 104 | } | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | |||
| 108 | return undef; | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | if (! exists $self->{_expr_parser}) { | ||
| 112 | # check the cache | ||
| 113 | if (! exists $expression_cache{$key}) { | ||
| 114 | $expression_cache{$key} = Template::Flute::Expression->new($key); | ||
| 115 | } | ||
| 116 | |||
| 117 | $self->{_expr_parser} = $expression_cache{$key}; | ||
| 118 | } | ||
| 119 | $ret = $self->{_expr_parser}->evaluate($self->{values}); | ||
| 120 | |||
| 121 | if ($ret) { | ||
| 122 | return 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | return 0; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | # container is visible if no value is specified | ||
| 130 | return 1; | ||
| 131 | } | ||
| 132 | |||
| 133 | =head1 AUTHOR | ||
| 134 | |||
| 135 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 136 | |||
| 137 | =head1 LICENSE AND COPYRIGHT | ||
| 138 | |||
| 139 | Copyright 2010-2019 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 140 | |||
| 141 | This program is free software; you can redistribute it and/or modify it | ||
| 142 | under the terms of either: the GNU General Public License as published | ||
| 143 | by the Free Software Foundation; or the Artistic License. | ||
| 144 | |||
| 145 | See http://dev.perl.org/licenses/ for more information. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | 1; |