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
|
|
72
|
use strict; |
|
10
|
|
|
|
|
23
|
|
|
10
|
|
|
|
|
302
|
|
10
|
10
|
|
|
10
|
|
55
|
use warnings; |
|
10
|
|
|
|
|
21
|
|
|
10
|
|
|
|
|
232
|
|
11
|
10
|
|
|
10
|
|
52
|
use Template::Alloy; |
|
10
|
|
|
|
|
28
|
|
|
10
|
|
|
|
|
63
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = $Template::Alloy::VERSION; |
14
|
|
|
|
|
|
|
our $AUTOLOAD; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
###----------------------------------------------------------------### |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
163
|
|
|
163
|
0
|
395
|
my $class = shift; |
20
|
163
|
|
50
|
|
|
536
|
my $self = shift || {}; |
21
|
163
|
50
|
|
|
|
590
|
die "Missing _template" if ! $self->{'_template'}; |
22
|
163
|
|
|
|
|
1186
|
return bless $self, $class; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
302
|
50
|
|
302
|
|
1447
|
sub _template { shift->{'_template'} || die "Missing _template" } |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub template { |
28
|
112
|
|
|
112
|
0
|
7974
|
my ($self, $name) = @_; |
29
|
112
|
|
66
|
|
|
331
|
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
|
35
|
my $self = shift; |
36
|
18
|
|
50
|
|
|
74
|
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
|
4363
|
my $self = shift; |
62
|
142
|
|
|
|
|
242
|
my $ref = shift; |
63
|
142
|
|
50
|
|
|
393
|
my $args = shift || {}; |
64
|
|
|
|
|
|
|
|
65
|
142
|
|
|
|
|
334
|
my $t = $self->_template; |
66
|
|
|
|
|
|
|
|
67
|
142
|
|
|
|
|
304
|
my $swap = $t->{'_vars'}; |
68
|
142
|
|
|
|
|
974
|
local $t->{'_vars'} = {%$swap}; |
69
|
|
|
|
|
|
|
|
70
|
142
|
|
|
|
|
826
|
$t->set_variable($_, $args->{$_}) for keys %$args; |
71
|
|
|
|
|
|
|
|
72
|
142
|
|
|
|
|
337
|
my $out = ''; # have temp item to allow clear to correctly clear |
73
|
142
|
|
|
|
|
269
|
eval { $t->_process($ref, $t->_vars, \$out) }; |
|
142
|
|
|
|
|
495
|
|
74
|
142
|
50
|
|
|
|
402
|
if (my $err = $@) { |
75
|
0
|
0
|
0
|
|
|
0
|
die $err if ! UNIVERSAL::can($err, 'type') || $err->type !~ /return/; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
142
|
|
|
|
|
945
|
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
|
169
|
my ($self, $type, $info) = @_; |
119
|
|
|
|
|
|
|
|
120
|
8
|
50
|
|
|
|
59
|
if (UNIVERSAL::can($type, 'type')) { |
|
|
50
|
|
|
|
|
|
121
|
0
|
|
|
|
|
0
|
die $type; |
122
|
|
|
|
|
|
|
} elsif (defined $info) { |
123
|
8
|
|
|
|
|
27
|
$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
|
|
|
sub DESTROY {} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
###----------------------------------------------------------------### |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
package Template::Alloy::_ContextStash; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
our $AUTOLOAD; |
138
|
|
|
|
|
|
|
|
139
|
9
|
50
|
|
9
|
|
71
|
sub _template { shift->{'_template'} || die "Missing _template" } |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub get { |
142
|
3
|
|
|
3
|
|
12
|
my ($self, $var) = @_; |
143
|
3
|
50
|
|
|
|
15
|
if (! ref $var) { |
144
|
3
|
50
|
|
|
|
18
|
if ($var =~ /^\w+$/) { $var = [$var, 0] } |
|
3
|
|
|
|
|
15
|
|
145
|
0
|
|
|
|
|
0
|
else { $var = $self->_template->parse_expr(\$var, {no_dots => 1}) } |
146
|
|
|
|
|
|
|
} |
147
|
3
|
|
|
|
|
13
|
return $self->_template->play_expr($var, {no_dots => 1}); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub set { |
151
|
3
|
|
|
3
|
|
14
|
my ($self, $var, $val) = @_; |
152
|
3
|
50
|
|
|
|
16
|
if (! ref $var) { |
153
|
3
|
50
|
|
|
|
19
|
if ($var =~ /^\w+$/) { $var = [$var, 0] } |
|
0
|
|
|
|
|
0
|
|
154
|
3
|
|
|
|
|
13
|
else { $var = $self->_template->parse_expr(\$var, {no_dots => 1}) } |
155
|
|
|
|
|
|
|
} |
156
|
3
|
|
|
|
|
16
|
$self->_template->set_variable($var, $val, {no_dots => 1}); |
157
|
3
|
|
|
|
|
19
|
return $val; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
0
|
|
|
0
|
|
|
sub AUTOLOAD { shift->_template->throw('not_implemented', "The method $AUTOLOAD has not been implemented") } |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
0
|
|
|
sub DESTROY {} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
###----------------------------------------------------------------### |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
1; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
__END__ |