line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::Template::Pluggable; |
2
|
10
|
|
|
10
|
|
62725
|
use base 'HTML::Template'; |
|
10
|
|
|
|
|
25
|
|
|
10
|
|
|
|
|
20150
|
|
3
|
10
|
|
|
10
|
|
247462
|
use Class::Trigger; |
|
10
|
|
|
|
|
15538
|
|
|
10
|
|
|
|
|
60
|
|
4
|
10
|
|
|
10
|
|
591
|
use vars (qw/$VERSION/); |
|
10
|
|
|
|
|
24
|
|
|
10
|
|
|
|
|
453
|
|
5
|
|
|
|
|
|
|
$VERSION = '0.17'; |
6
|
10
|
|
|
10
|
|
55
|
use warnings; |
|
10
|
|
|
|
|
20
|
|
|
10
|
|
|
|
|
569
|
|
7
|
10
|
|
|
10
|
|
103
|
use strict; |
|
10
|
|
|
|
|
19
|
|
|
10
|
|
|
|
|
282
|
|
8
|
10
|
|
|
10
|
|
76
|
use Carp; |
|
10
|
|
|
|
|
24
|
|
|
10
|
|
|
|
|
7777
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 NAME |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
HTML::Template::Pluggable - Extends HTML::Template with plugin support |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Just use this module instead of HTML::Template, then use any plugins, |
19
|
|
|
|
|
|
|
and go on with life. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
use HTML::Template::Pluggable; |
22
|
|
|
|
|
|
|
use HTML::Template::Plugin::Dot; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# Everything works the same, except for functionality that plugins add. |
25
|
|
|
|
|
|
|
my $t = HTML::Template::Pluggable->new(); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 THE GOAL |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Ideally we'd like to see this functionality merged into HTML::Template, |
30
|
|
|
|
|
|
|
and turn this into a null sub-class. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=head1 STATUS |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
The design of the plugin system is still in progress. Right now we have just |
35
|
|
|
|
|
|
|
two triggers, in param and output. The name and function of this may change, |
36
|
|
|
|
|
|
|
and we would like to add triggers in new() and other methods when the need |
37
|
|
|
|
|
|
|
arises. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
All we promise for now is to keep L compatible. |
40
|
|
|
|
|
|
|
Please get in touch if you have suggestions with feedback on designing the |
41
|
|
|
|
|
|
|
plugin system if you would like to contribute. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub param { |
46
|
129
|
|
|
129
|
1
|
109867
|
my $self = shift; |
47
|
129
|
|
|
|
|
220
|
my $options = $self->{options}; |
48
|
129
|
|
|
|
|
189
|
my $param_map = $self->{param_map}; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# the no-parameter case - return list of parameters in the template. |
51
|
129
|
50
|
|
|
|
335
|
return keys(%$param_map) unless scalar(@_); |
52
|
|
|
|
|
|
|
|
53
|
129
|
|
|
|
|
180
|
my $first = shift; |
54
|
129
|
|
|
|
|
187
|
my $type = ref $first; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# the one-parameter case - could be a parameter value request or a |
57
|
|
|
|
|
|
|
# hash-ref. |
58
|
129
|
100
|
66
|
|
|
440
|
if (!scalar(@_) and !length($type)) { |
59
|
4
|
50
|
|
|
|
15
|
my $param = $options->{case_sensitive} ? $first : lc $first; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# check for parameter existence |
62
|
4
|
50
|
33
|
|
|
36
|
$options->{die_on_bad_params} and !exists($param_map->{$param}) and |
63
|
|
|
|
|
|
|
croak("HTML::Template : Attempt to get nonexistent parameter '$param' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params set => 1)"); |
64
|
|
|
|
|
|
|
|
65
|
4
|
50
|
33
|
|
|
26
|
return undef unless (exists($param_map->{$param}) and |
66
|
|
|
|
|
|
|
defined($param_map->{$param})); |
67
|
|
|
|
|
|
|
|
68
|
4
|
50
|
|
|
|
13
|
return ${$param_map->{$param}} if |
|
4
|
|
|
|
|
21
|
|
69
|
|
|
|
|
|
|
(ref($param_map->{$param}) eq 'HTML::Template::VAR'); |
70
|
0
|
|
|
|
|
0
|
return $param_map->{$param}[HTML::Template::LOOP::PARAM_SET]; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
125
|
100
|
|
|
|
269
|
if (!scalar(@_)) { |
74
|
58
|
0
|
0
|
|
|
116
|
croak("HTML::Template->param() : Single reference arg to param() must be a hash-ref! You gave me a $type.") |
|
|
|
33
|
|
|
|
|
75
|
|
|
|
|
|
|
unless $type eq 'HASH' or |
76
|
|
|
|
|
|
|
(ref($first) and UNIVERSAL::isa($first, 'HASH')); |
77
|
58
|
|
|
|
|
177
|
push(@_, %$first); |
78
|
|
|
|
|
|
|
} else { |
79
|
67
|
|
|
|
|
157
|
unshift(@_, $first); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
125
|
50
|
|
|
|
421
|
croak("HTML::Template->param() : You gave me an odd number of parameters to param()!") |
83
|
|
|
|
|
|
|
unless ((@_ % 2) == 0); |
84
|
|
|
|
|
|
|
|
85
|
125
|
|
|
|
|
609
|
$self->call_trigger('middle_param', @_); |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# strangely, changing this to a "while(@_) { shift, shift }" type |
88
|
|
|
|
|
|
|
# loop causes perl 5.004_04 to die with some nonsense about a |
89
|
|
|
|
|
|
|
# read-only value. |
90
|
120
|
|
|
|
|
1607
|
for (my $x = 0; $x <= $#_; $x += 2) { |
91
|
123
|
50
|
|
|
|
368
|
my $param = $options->{case_sensitive} ? $_[$x] : lc $_[$x]; |
92
|
123
|
|
|
|
|
191
|
my $value = $_[($x + 1)]; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# necessary to cooperate with plugin system |
95
|
123
|
100
|
|
|
|
857
|
next if $self->{param_map_done}{$param}; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
# check that this param exists in the template |
98
|
15
|
100
|
66
|
|
|
190
|
$options->{die_on_bad_params} and !exists($param_map->{$param}) and |
99
|
|
|
|
|
|
|
croak("HTML::Template : Attempt to set nonexistent parameter '$param' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params => 1)"); |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# if we're not going to die from bad param names, we need to ignore |
102
|
|
|
|
|
|
|
# them... |
103
|
14
|
50
|
|
|
|
46
|
next unless (exists($param_map->{$param})); |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# figure out what we've got, taking special care to allow for |
106
|
|
|
|
|
|
|
# objects that are compatible underneath. |
107
|
14
|
|
|
|
|
20
|
my $value_type = ref($value); |
108
|
14
|
100
|
66
|
|
|
107
|
if (defined($value_type) and length($value_type) and ($value_type eq 'ARRAY' or ((ref($value) !~ /^(CODE)|(HASH)|(SCALAR)$/) and $value->isa('ARRAY')))) { |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
109
|
2
|
50
|
|
|
|
9
|
(ref($param_map->{$param}) eq 'HTML::Template::LOOP') or |
110
|
|
|
|
|
|
|
croak("HTML::Template::param() : attempt to set parameter '$param' with an array ref - parameter is not a TMPL_LOOP!"); |
111
|
2
|
|
|
|
|
4
|
$param_map->{$param}[HTML::Template::LOOP::PARAM_SET] = [@{$value}]; |
|
2
|
|
|
|
|
14
|
|
112
|
|
|
|
|
|
|
} else { |
113
|
12
|
50
|
|
|
|
53
|
(ref($param_map->{$param}) eq 'HTML::Template::VAR') or |
114
|
|
|
|
|
|
|
croak("HTML::Template::param() : attempt to set parameter '$param' with a scalar - parameter is not a TMPL_VAR!"); |
115
|
12
|
|
|
|
|
26
|
${$param_map->{$param}} = $value; |
|
12
|
|
|
|
|
88
|
|
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub output |
122
|
|
|
|
|
|
|
{ |
123
|
106
|
|
|
106
|
1
|
1844
|
my $self = shift; |
124
|
106
|
|
|
|
|
7286
|
$self->call_trigger('before_output', @_); |
125
|
|
|
|
|
|
|
|
126
|
106
|
|
|
|
|
4432
|
$self->SUPER::output(@_); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 WRITING PLUGINS |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
HTML::Template offers a plugin system which allows developers to extend the |
133
|
|
|
|
|
|
|
functionality in significant ways without creating a creating a sub-class, |
134
|
|
|
|
|
|
|
which might be impossible to use in combination with another sub-class |
135
|
|
|
|
|
|
|
extension. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Currently, two triggers have been made available to alter how the values of |
138
|
|
|
|
|
|
|
TMPL_VARs are set. If more hooks are needed to implement your own plugin idea, |
139
|
|
|
|
|
|
|
it may be feasible to add them-- check the FAQ then ask about it on the list. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
L is used to provide plugins. Basically, you can just: |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
HTML::Template->add_trigger('middle_param', \&trigger); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
A good place to add one is in your plugin's C subroutine: |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
package HTML::Template::Plugin::MyPlugin; |
148
|
|
|
|
|
|
|
use base 'Exporter'; |
149
|
|
|
|
|
|
|
sub import { |
150
|
|
|
|
|
|
|
HTML::Template->add_trigger('middle_param', \&dot_notation); |
151
|
|
|
|
|
|
|
goto &Exporter::import; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 TRIGGER LOCATIONS |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=over 4 |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item param |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
We have added one trigger location to this method, named C. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
# in a Plugin's import() routine. |
163
|
|
|
|
|
|
|
HTML::Template->add_trigger('middle_param', \&_set_tmpl_var_with_dot ); |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
This sets a callback which is executed in param() with all of the same |
166
|
|
|
|
|
|
|
arguments. It is only useful for altering how /setting/ params works. |
167
|
|
|
|
|
|
|
The logic to read a param is unaffected. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
It can set any TMPL_VAR values before the normal param logic kicks in. To do |
170
|
|
|
|
|
|
|
this, C<$self-E{param_map}> is modified as can be seen from source in |
171
|
|
|
|
|
|
|
HTML::Template::param(). However, it must obey the following convention of |
172
|
|
|
|
|
|
|
setting $self->{param_map_done}{$param_name} for each param that is set. |
173
|
|
|
|
|
|
|
C<$param_name> would be a key from C<$self-E{param_map}>. This notifies the |
174
|
|
|
|
|
|
|
other plugins and the core param() routine to skip trying to set this value. |
175
|
|
|
|
|
|
|
$self->{param_map_done} is reset with each call to param(), so that like with a |
176
|
|
|
|
|
|
|
hash, you have the option to reset a param later with the same name. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item output |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
One trigger location here: C. |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
HTML::Template->add_trigger('before_output', \&_last_chance_params ); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
This sets a callback which is executed right before output is generated. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=back |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head1 SEE ALSO |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=over 4 |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=item o |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
L - Add Template Toolkit's magic dot notation to |
195
|
|
|
|
|
|
|
HTML::Template. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=back |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 AUTHOR |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Mark Stosberg, C<< >> |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 BUGS |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
206
|
|
|
|
|
|
|
C, or through the web interface at |
207
|
|
|
|
|
|
|
L. I will be notified, and then you'll automatically |
208
|
|
|
|
|
|
|
be notified of progress on your bug as I make changes. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head1 Copyright & License |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Copyright 2006 Mark Stosberg, All Rights Reserved. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
215
|
|
|
|
|
|
|
under the same terms as Perl itself. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=cut |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
1; # End of HTML::Template::Pluggable |