line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebNano::Renderer::TTiny; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
3
|
|
|
3
|
|
25290
|
$WebNano::Renderer::TTiny::VERSION = '0.007'; |
4
|
|
|
|
|
|
|
} |
5
|
3
|
|
|
3
|
|
28
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
85
|
|
6
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
77
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
2575
|
use Template::Tiny; |
|
3
|
|
|
|
|
4377
|
|
|
3
|
|
|
|
|
111
|
|
9
|
3
|
|
|
3
|
|
938
|
use Object::Tiny::RW qw/ root _tt_tiny INCLUDE_PATH TEMPLATE_EXTENSION /; |
|
3
|
|
|
|
|
324
|
|
|
3
|
|
|
|
|
27
|
|
10
|
3
|
|
|
3
|
|
961
|
use File::Spec; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
2034
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub _to_list { |
14
|
29
|
100
|
|
29
|
|
192
|
if( ref $_[0] ){ |
|
|
50
|
|
|
|
|
|
15
|
13
|
|
|
|
|
16
|
return @{ $_[0] }; |
|
13
|
|
|
|
|
36
|
|
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
elsif( ! defined $_[0] ){ |
18
|
0
|
|
|
|
|
0
|
return (); |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
else{ |
21
|
16
|
|
|
|
|
36
|
return $_[0]; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub render { |
26
|
23
|
|
|
23
|
1
|
1954
|
my( $self, %vars ) = @_; |
27
|
23
|
|
|
|
|
47
|
my $c = $vars{c}; |
28
|
|
|
|
|
|
|
|
29
|
23
|
|
|
|
|
30
|
my @search_path; |
30
|
23
|
50
|
|
|
|
65
|
if( $c ){ |
31
|
23
|
|
|
|
|
36
|
my $path = ref $c; |
32
|
23
|
|
|
|
|
131
|
$path =~ s/.*::Controller(::)?//; |
33
|
23
|
|
|
|
|
62
|
$path =~ s{::}{/}; |
34
|
23
|
|
|
|
|
30
|
@search_path = ( $path, @{ $c->template_search_path }); |
|
23
|
|
|
|
|
464
|
|
35
|
|
|
|
|
|
|
} |
36
|
23
|
50
|
|
|
|
335
|
if( !@search_path ){ |
37
|
0
|
|
|
|
|
0
|
@search_path = ( '' ); |
38
|
|
|
|
|
|
|
} |
39
|
23
|
|
|
|
|
53
|
my $template = $vars{template}; |
40
|
23
|
100
|
|
|
|
54
|
if( !$template ){ |
41
|
2
|
|
|
|
|
16
|
my @caller = caller(2); |
42
|
2
|
|
|
|
|
4
|
$template = $caller[3]; |
43
|
2
|
|
|
|
|
8
|
$template =~ s/_action$//; |
44
|
2
|
|
|
|
|
7
|
$template =~ s/^.*:://; |
45
|
2
|
50
|
|
|
|
51
|
$template .= '.' . $self->TEMPLATE_EXTENSION if $self->TEMPLATE_EXTENSION; |
46
|
|
|
|
|
|
|
} |
47
|
23
|
|
|
|
|
44
|
my $full_template; |
48
|
|
|
|
|
|
|
LOOP: |
49
|
23
|
|
|
|
|
45
|
for my $path ( @search_path ){ |
50
|
34
|
|
|
|
|
37
|
my $to_check; |
51
|
34
|
100
|
100
|
|
|
814
|
if( !$self->root || File::Spec->file_name_is_absolute( $path ) ){ |
52
|
6
|
|
|
|
|
103
|
$to_check = File::Spec->catfile( $path, $template ); |
53
|
6
|
100
|
|
|
|
157
|
if( -f $to_check ){ |
54
|
4
|
|
|
|
|
40
|
$full_template = $to_check; |
55
|
4
|
|
|
|
|
10
|
last LOOP; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else{ |
59
|
28
|
|
|
|
|
1038
|
for my $root ( _to_list( $self->root ) ){ |
60
|
40
|
|
|
|
|
460
|
$to_check = File::Spec->catfile( $root, $path, $template ); |
61
|
40
|
100
|
|
|
|
862
|
if( -f $to_check ){ |
62
|
18
|
|
|
|
|
29
|
$full_template = $to_check; |
63
|
18
|
|
|
|
|
47
|
last LOOP; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
23
|
|
|
|
|
40
|
my @static_search_path; |
69
|
23
|
100
|
|
|
|
55
|
if( !$full_template ){ |
70
|
1
|
|
|
|
|
22
|
@static_search_path = _to_list( $self->INCLUDE_PATH ); |
71
|
|
|
|
|
|
|
STATIC_LOOP: |
72
|
1
|
|
|
|
|
3
|
for my $path ( @static_search_path ){ |
73
|
1
|
|
|
|
|
1
|
my $to_check; |
74
|
1
|
|
|
|
|
8
|
$to_check = File::Spec->catfile( $path, $template ); |
75
|
1
|
50
|
|
|
|
20
|
if( -f $to_check ){ |
76
|
1
|
|
|
|
|
2
|
$full_template = $to_check; |
77
|
1
|
|
|
|
|
3
|
last STATIC_LOOP; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
23
|
50
|
|
|
|
55
|
die "Cannot find $template in search path: @search_path, @static_search_path" if !defined $full_template; |
82
|
23
|
50
|
|
|
|
959
|
open my $fh, $full_template or die "Cannot read from $full_template: $!"; |
83
|
23
|
|
|
|
|
39
|
my $string = do { local $/; <$fh> }; |
|
23
|
|
|
|
|
84
|
|
|
23
|
|
|
|
|
794
|
|
84
|
23
|
100
|
|
|
|
567
|
if( !$self->_tt_tiny ){ |
85
|
5
|
|
|
|
|
61
|
$self->_tt_tiny( Template::Tiny->new() ); |
86
|
|
|
|
|
|
|
} |
87
|
23
|
|
|
|
|
264
|
my $out; |
88
|
23
|
|
|
|
|
472
|
$self->_tt_tiny->process( \$string, \%vars, \$out ); |
89
|
23
|
|
|
|
|
2540
|
return $out; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
1; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=pod |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 NAME |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
WebNano::Renderer::TTiny - Dynamic search paths for Template::Tiny |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 VERSION |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
version 0.007 |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 SYNOPSIS |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
in MyApp.pm: |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$self->renderer( |
111
|
|
|
|
|
|
|
WebNano::Renderer::TTiny->new( root => 't/data/templates' ) |
112
|
|
|
|
|
|
|
); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
in MyApp::Controller: |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
return $self->render( template => 'some_template.tt', some_var => 'some_value ); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 DESCRIPTION |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
This is a wrapper around L |
121
|
|
|
|
|
|
|
- 'Template Toolkit reimplemented in as little code as possible'. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
The only public method here is render - it expects as input a hash with the |
124
|
|
|
|
|
|
|
following data: |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=over |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item template - the name of the template file |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item c - the controller |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=back |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
The template is then looked for in the directories in C and in |
135
|
|
|
|
|
|
|
directories constructed dynamically from the paths in C and the controller |
136
|
|
|
|
|
|
|
name. For example if 'root' contains C<[ 'template', 'additional_templates' ]> |
137
|
|
|
|
|
|
|
and the controller name is C then the template |
138
|
|
|
|
|
|
|
will be looked for in C and |
139
|
|
|
|
|
|
|
C. This mechanism is designed so that it is |
140
|
|
|
|
|
|
|
possible for a way of subclassing templates along with subclassing controllers. |
141
|
|
|
|
|
|
|
If this is too complicated - you can provide no value for the C attribute |
142
|
|
|
|
|
|
|
and use only C. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
When the template is found - the C method on the internal |
145
|
|
|
|
|
|
|
C object is called. A reference to the whole hash passed to |
146
|
|
|
|
|
|
|
C is passed to the C call - so that all the values are |
147
|
|
|
|
|
|
|
available in the template itself. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
If no template name is passed - then it is guessed from the name of the |
150
|
|
|
|
|
|
|
controller method that called C (this is done using L) and the |
151
|
|
|
|
|
|
|
C attribute. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 ATTRIBUTES and METHODS |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 render |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 INCLUDE_PATH |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Static list of template search directories. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 root |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
List of directories that are dynamically concatenated with controller names to form |
164
|
|
|
|
|
|
|
a dynamic search list of template directories. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
You can use INCLUDE_PATH or root or both. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 TEMPLATE_EXTENSION |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Postfix added to action name to form the template name ( for example 'edit.tt' |
171
|
|
|
|
|
|
|
from action 'edit' and TEMPLATE_EXTENSION 'tt' ). |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 AUTHOR |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
Zbigniew Lukasiak |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
This software is Copyright (c) 2010 by Zbigniew Lukasiak . |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
This is free software, licensed under: |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=cut |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
__END__ |