| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Template::Alloy::Context; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | =head1 NAME | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | Template::Alloy::Context - Provide a TT style context | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =cut | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 10 |  |  | 10 |  | 62 | use strict; | 
|  | 10 |  |  |  |  | 23 |  | 
|  | 10 |  |  |  |  | 375 |  | 
| 10 | 10 |  |  | 10 |  | 54 | use warnings; | 
|  | 10 |  |  |  |  | 21 |  | 
|  | 10 |  |  |  |  | 293 |  | 
| 11 | 10 |  |  | 10 |  | 58 | use Template::Alloy; | 
|  | 10 |  |  |  |  | 20 |  | 
|  | 10 |  |  |  |  | 106 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $VERSION = $Template::Alloy::VERSION; | 
| 14 | 10 |  |  | 10 |  | 59 | use vars qw($AUTOLOAD); | 
|  | 10 |  |  |  |  | 14 |  | 
|  | 10 |  |  |  |  | 10923 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | ###----------------------------------------------------------------### | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub new { | 
| 19 | 163 |  |  | 163 | 0 | 283 | my $class = shift; | 
| 20 | 163 |  | 50 |  |  | 636 | my $self  = shift || {}; | 
| 21 | 163 | 50 |  |  |  | 491 | die "Missing _template" if ! $self->{'_template'}; | 
| 22 | 163 |  |  |  |  | 2161 | return bless $self, $class; | 
| 23 |  |  |  |  |  |  | } | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 302 | 50 |  | 302 |  | 1951 | sub _template { shift->{'_template'} || die "Missing _template" } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub template { | 
| 28 | 112 |  |  | 112 | 0 | 5982 | my ($self, $name) = @_; | 
| 29 | 112 |  | 66 |  |  | 280 | return $self->_template->{'BLOCKS'}->{$name} || $self->_template->load_template($name); | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 0 |  |  | 0 | 0 | 0 | sub config { shift->_template } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | sub stash { | 
| 35 | 18 |  |  | 18 | 0 | 33 | my $self = shift; | 
| 36 | 18 |  | 50 |  |  | 100 | return $self->{'stash'} ||= bless {_template => $self->_template}, 'Template::Alloy::_ContextStash'; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub insert { | 
| 40 | 0 |  |  | 0 | 0 | 0 | my ($self, $file) = @_;; | 
| 41 | 0 |  |  |  |  | 0 | my $t = $self->_template; | 
| 42 | 0 |  |  |  |  | 0 | my $ref = $t->slurp($t->include_filename($file)); | 
| 43 | 0 |  |  |  |  | 0 | return $$ref; | 
| 44 |  |  |  |  |  |  | } | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 0 |  |  | 0 | 0 | 0 | sub eval_perl { shift->_template->{'EVAL_PERL'} } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub process { | 
| 49 | 0 |  |  | 0 | 0 | 0 | my $self = shift; | 
| 50 | 0 |  |  |  |  | 0 | my $ref  = shift; | 
| 51 | 0 |  | 0 |  |  | 0 | my $args = shift || {}; | 
| 52 |  |  |  |  |  |  |  | 
| 53 | 0 |  |  |  |  | 0 | $self->_template->set_variable($_, $args->{$_}) for keys %$args; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 0 |  |  |  |  | 0 | my $out  = ''; | 
| 56 | 0 |  |  |  |  | 0 | $self->_template->_process($ref, $self->_template->_vars, \$out); | 
| 57 | 0 |  |  |  |  | 0 | return $out; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | sub include { | 
| 61 | 142 |  |  | 142 | 0 | 3597 | my $self = shift; | 
| 62 | 142 |  |  |  |  | 183 | my $ref  = shift; | 
| 63 | 142 |  | 50 |  |  | 505 | my $args = shift || {}; | 
| 64 |  |  |  |  |  |  |  | 
| 65 | 142 |  |  |  |  | 290 | my $t = $self->_template; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 142 |  |  |  |  | 283 | my $swap = $t->{'_vars'}; | 
| 68 | 142 |  |  |  |  | 1002 | local $t->{'_vars'} = {%$swap}; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 142 |  |  |  |  | 1486 | $t->set_variable($_, $args->{$_}) for keys %$args; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 142 |  |  |  |  | 275 | my $out = ''; # have temp item to allow clear to correctly clear | 
| 73 | 142 |  |  |  |  | 176 | eval { $t->_process($ref, $t->_vars, \$out) }; | 
|  | 142 |  |  |  |  | 846 |  | 
| 74 | 142 | 50 |  |  |  | 599 | if (my $err = $@) { | 
| 75 | 0 | 0 | 0 |  |  | 0 | die $err if ! UNIVERSAL::can($err, 'type') || $err->type !~ /return/; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 142 |  |  |  |  | 1578 | return $out; | 
| 79 |  |  |  |  |  |  | } | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | sub define_filter { | 
| 82 | 0 |  |  | 0 | 0 | 0 | my ($self, $name, $filter, $is_dynamic) = @_; | 
| 83 | 0 | 0 |  |  |  | 0 | $filter = [ $filter, 1 ] if $is_dynamic; | 
| 84 | 0 |  |  |  |  | 0 | $self->define_vmethod('filter', $name, $filter); | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub filter { | 
| 88 | 0 |  |  | 0 | 0 | 0 | my ($self, $name, $args, $alias) = @_; | 
| 89 | 0 |  |  |  |  | 0 | my $t = $self->_template; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 0 |  |  |  |  | 0 | my $filter; | 
| 92 | 0 | 0 | 0 |  |  | 0 | if (! ref $name) { | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 93 | 0 |  | 0 |  |  | 0 | $filter = $t->{'FILTERS'}->{$name} || $Template::Alloy::FILTER_OPS->{$name} || $Template::Alloy::SCALAR_OPS->{$name}; | 
| 94 | 0 | 0 |  |  |  | 0 | $t->throw('filter', $name) if ! $filter; | 
| 95 |  |  |  |  |  |  | } elsif (UNIVERSAL::isa($name, 'CODE') || UNIVERSAL::isa($name, 'ARRAY')) { | 
| 96 | 0 |  |  |  |  | 0 | $filter = $name; | 
| 97 |  |  |  |  |  |  | } elsif (UNIVERSAL::can($name, 'factory')) { | 
| 98 | 0 |  | 0 |  |  | 0 | $filter = $name->factory || $t->throw($name->error); | 
| 99 |  |  |  |  |  |  | } else { | 
| 100 | 0 |  |  |  |  | 0 | $t->throw('undef', "$name: filter not found"); | 
| 101 |  |  |  |  |  |  | } | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 0 | 0 | 0 |  |  | 0 | if (UNIVERSAL::isa($filter, 'ARRAY')) { | 
|  |  | 0 |  |  |  |  |  | 
| 104 | 0 | 0 |  |  |  | 0 | $filter = ($filter->[1]) ? $filter->[0]->($t->context, @$args) : $filter->[0]; | 
| 105 |  |  |  |  |  |  | } elsif ($args && @$args) { | 
| 106 | 0 |  |  |  |  | 0 | my $sub = $filter; | 
| 107 | 0 |  |  | 0 |  | 0 | $filter = sub { $sub->(shift, @$args) }; | 
|  | 0 |  |  |  |  | 0 |  | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 | 0 | 0 |  |  |  | 0 | $t->{'FILTERS'}->{$alias} = $filter if $alias; | 
| 111 |  |  |  |  |  |  |  | 
| 112 | 0 |  |  |  |  | 0 | return $filter; | 
| 113 |  |  |  |  |  |  | } | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 0 |  |  | 0 | 0 | 0 | sub define_vmethod { shift->_template->define_vmethod(@_) } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | sub throw { | 
| 118 | 8 |  |  | 8 | 0 | 158 | my ($self, $type, $info) = @_; | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 8 | 50 |  |  |  | 55 | if (UNIVERSAL::can($type, 'type')) { | 
|  |  | 50 |  |  |  |  |  | 
| 121 | 0 |  |  |  |  | 0 | die $type; | 
| 122 |  |  |  |  |  |  | } elsif (defined $info) { | 
| 123 | 8 |  |  |  |  | 25 | $self->_template->throw($type, $info); | 
| 124 |  |  |  |  |  |  | } else { | 
| 125 | 0 |  |  |  |  | 0 | $self->_template->throw('undef', $type); | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 0 |  |  | 0 |  | 0 | sub AUTOLOAD { shift->_template->throw('not_implemented', "The method $AUTOLOAD has not been implemented") } | 
| 130 |  |  |  |  |  |  |  | 
| 131 | 0 |  |  | 0 |  | 0 | sub DESTROY {} | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | ###----------------------------------------------------------------### | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | package Template::Alloy::_ContextStash; | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 10 |  |  | 10 |  | 68 | use vars qw($AUTOLOAD); | 
|  | 10 |  |  |  |  | 23 |  | 
|  | 10 |  |  |  |  | 3701 |  | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 9 | 50 |  | 9 |  | 72 | sub _template { shift->{'_template'} || die "Missing _template" } | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | sub get { | 
| 142 | 3 |  |  | 3 |  | 10 | my ($self, $var) = @_; | 
| 143 | 3 | 50 |  |  |  | 11 | if (! ref $var) { | 
| 144 | 3 | 50 |  |  |  | 21 | if ($var =~ /^\w+$/) {  $var = [$var, 0] } | 
|  | 3 |  |  |  |  | 8 |  | 
| 145 | 0 |  |  |  |  | 0 | else {                  $var = $self->_template->parse_expr(\$var, {no_dots => 1}) } | 
| 146 |  |  |  |  |  |  | } | 
| 147 | 3 |  |  |  |  | 15 | return $self->_template->play_expr($var, {no_dots => 1}); | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | sub set { | 
| 151 | 3 |  |  | 3 |  | 11 | my ($self, $var, $val) = @_; | 
| 152 | 3 | 50 |  |  |  | 10 | if (! ref $var) { | 
| 153 | 3 | 50 |  |  |  | 13 | if ($var =~ /^\w+$/) {  $var = [$var, 0] } | 
|  | 0 |  |  |  |  | 0 |  | 
| 154 | 3 |  |  |  |  | 10 | else {                  $var = $self->_template->parse_expr(\$var, {no_dots => 1}) } | 
| 155 |  |  |  |  |  |  | } | 
| 156 | 3 |  |  |  |  | 15 | $self->_template->set_variable($var, $val, {no_dots => 1}); | 
| 157 | 3 |  |  |  |  | 23 | return $val; | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  |  | 
| 160 | 0 |  |  | 0 |  |  | sub AUTOLOAD { shift->_template->throw('not_implemented', "The method $AUTOLOAD has not been implemented") } | 
| 161 |  |  |  |  |  |  |  | 
| 162 | 0 |  |  | 0 |  |  | sub DESTROY {} | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | ###----------------------------------------------------------------### | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | 1; | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | __END__ |