line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer::Template::TemplateToolkit; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:SUKRIA'; |
3
|
|
|
|
|
|
|
#ABSTRACT: Template Toolkit wrapper for Dancer |
4
|
|
|
|
|
|
|
$Dancer::Template::TemplateToolkit::VERSION = '1.3514_04'; # TRIAL |
5
|
|
|
|
|
|
|
$Dancer::Template::TemplateToolkit::VERSION = '1.351404'; |
6
|
3
|
|
|
3
|
|
2979
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
85
|
|
7
|
3
|
|
|
3
|
|
15
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
72
|
|
8
|
3
|
|
|
3
|
|
14
|
use Carp; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
205
|
|
9
|
3
|
|
|
3
|
|
446
|
use Dancer::Config 'setting'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
138
|
|
10
|
3
|
|
|
3
|
|
46
|
use Dancer::ModuleLoader; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
86
|
|
11
|
3
|
|
|
3
|
|
15
|
use Dancer::Exception qw(:all); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
376
|
|
12
|
|
|
|
|
|
|
|
13
|
3
|
|
|
3
|
|
19
|
use base 'Dancer::Template::Abstract'; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
2877
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $_engine; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub init { |
18
|
7
|
|
|
7
|
1
|
16
|
my ($self) = @_; |
19
|
|
|
|
|
|
|
|
20
|
7
|
|
50
|
|
|
29
|
my $class = $self->config->{subclass} || "Template"; |
21
|
7
|
100
|
100
|
|
|
52
|
raise core_template => "$class is needed by Dancer::Template::TemplateToolkit" |
22
|
|
|
|
|
|
|
if !$class->can("process") and !Dancer::ModuleLoader->load($class); |
23
|
|
|
|
|
|
|
|
24
|
6
|
|
50
|
|
|
34
|
my $charset = setting('charset') || ''; |
25
|
6
|
50
|
|
|
|
23
|
my @encoding = length($charset) ? ( ENCODING => $charset ) : (); |
26
|
|
|
|
|
|
|
|
27
|
6
|
|
|
|
|
13
|
my $is_subclass = $class ne 'Template'; |
28
|
|
|
|
|
|
|
|
29
|
6
|
50
|
|
|
|
21
|
my @anycase = $is_subclass ? () : ( ANYCASE => 1 ); |
30
|
6
|
50
|
|
|
|
18
|
my @absolute = $is_subclass ? () : ( ABSOLUTE => 1 ); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
my @inc_path = $is_subclass ? () |
33
|
6
|
50
|
66
|
|
|
21
|
: ( INCLUDE_PATH => $self->config->{INCLUDE_PATH} || setting('views') ); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $start_tag = $is_subclass |
36
|
|
|
|
|
|
|
? $self->config->{start_tag} |
37
|
6
|
50
|
50
|
|
|
21
|
: $self->config->{start_tag} || '<%'; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $stop_tag = $is_subclass |
40
|
|
|
|
|
|
|
? $self->config->{stop_tag} || $self->config->{end_tag} |
41
|
6
|
50
|
0
|
|
|
22
|
: $self->config->{stop_tag} || $self->config->{end_tag} || '%>'; |
|
|
|
50
|
|
|
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# TT expects quotemeta()'ed values here to be used as-is within |
44
|
|
|
|
|
|
|
# its regexp-based tokenizer. To support existing Dancer users who |
45
|
|
|
|
|
|
|
# prefer the default TT tags and who've already figured this out, |
46
|
|
|
|
|
|
|
# let's skip this if the tags are already ok. |
47
|
|
|
|
|
|
|
# Just FYI: TT hardcodes '\[%' and '%\]' as default. |
48
|
|
|
|
|
|
|
# |
49
|
6
|
|
|
|
|
11
|
my @start = (); |
50
|
6
|
50
|
|
|
|
14
|
if (defined $start_tag) { |
51
|
6
|
50
|
33
|
|
|
40
|
@start = ( START_TAG => $start_tag eq '\[%' || $start_tag eq '\[\%' |
52
|
|
|
|
|
|
|
? $start_tag |
53
|
|
|
|
|
|
|
: quotemeta($start_tag) |
54
|
|
|
|
|
|
|
); |
55
|
|
|
|
|
|
|
} |
56
|
6
|
|
|
|
|
33
|
my @stop = (); |
57
|
6
|
50
|
|
|
|
18
|
if (defined $stop_tag) { |
58
|
6
|
50
|
33
|
|
|
30
|
@stop = ( END_TAG => $stop_tag eq '%\]' || $stop_tag eq '\%\]' |
59
|
|
|
|
|
|
|
? $stop_tag |
60
|
|
|
|
|
|
|
: quotemeta($stop_tag) |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
} |
63
|
6
|
|
|
|
|
12
|
my @embedded = (); |
64
|
6
|
50
|
|
|
|
16
|
if ($self->config->{embedded_templates}) { |
65
|
0
|
0
|
|
|
|
0
|
Dancer::ModuleLoader->load('Template::Provider::FromDATA') |
66
|
|
|
|
|
|
|
or croak "The Package Template::Provider::FromDATA must be installed to use embedded_templates"; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
0
|
@embedded = ( LOAD_TEMPLATES => [Template::Provider::FromDATA->new()] ); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $tt_config = { |
72
|
|
|
|
|
|
|
@anycase, |
73
|
|
|
|
|
|
|
@absolute, |
74
|
|
|
|
|
|
|
@encoding, |
75
|
|
|
|
|
|
|
@inc_path, |
76
|
|
|
|
|
|
|
@start, |
77
|
|
|
|
|
|
|
@stop, |
78
|
|
|
|
|
|
|
@embedded, |
79
|
6
|
|
|
|
|
15
|
%{$self->config}, |
|
6
|
|
|
|
|
13
|
|
80
|
|
|
|
|
|
|
}; |
81
|
|
|
|
|
|
|
|
82
|
6
|
|
|
|
|
70
|
$_engine = $class->new(%$tt_config); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
sub set_wrapper { |
86
|
4
|
|
|
4
|
1
|
2278
|
my ($self, $when, $file) = @_; |
87
|
4
|
|
|
|
|
9
|
my $wrappers = $_engine->{SERVICE}->{WRAPPER}; |
88
|
4
|
100
|
|
|
|
13
|
unless (defined $file) { |
89
|
1
|
|
|
|
|
3
|
$file = $when; |
90
|
1
|
|
|
|
|
3
|
my @orig = @$wrappers; |
91
|
1
|
|
|
|
|
3
|
$self->{orig_wrappers} = \@orig; |
92
|
1
|
|
|
|
|
2
|
@$wrappers = ($file); |
93
|
1
|
|
|
|
|
3
|
return; |
94
|
|
|
|
|
|
|
} |
95
|
3
|
100
|
|
|
|
13
|
if ($when eq 'outer') { |
|
|
100
|
|
|
|
|
|
96
|
1
|
|
|
|
|
3
|
unshift @$wrappers => $file; |
97
|
|
|
|
|
|
|
} elsif ($when eq 'inner') { |
98
|
1
|
|
|
|
|
3
|
push @$wrappers => $file; |
99
|
|
|
|
|
|
|
} else { |
100
|
1
|
|
|
|
|
8
|
raise core_template => "'$when' isn't a valid identifier"; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub reset_wrapper { |
105
|
1
|
|
|
1
|
1
|
589
|
my ($self) = @_; |
106
|
1
|
|
|
|
|
4
|
my $wrappers = $_engine->{SERVICE}->{WRAPPER}; |
107
|
1
|
|
50
|
|
|
4
|
my $orig = $self->{orig_wrappers} || []; |
108
|
1
|
|
|
|
|
4
|
my @old = @$wrappers; |
109
|
1
|
|
|
|
|
3
|
@$wrappers = @$orig; |
110
|
1
|
|
|
|
|
3
|
return @old; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub unset_wrapper { |
114
|
3
|
|
|
3
|
1
|
1529
|
my ($self, $when) = @_; |
115
|
3
|
|
|
|
|
8
|
my $wrappers = $_engine->{SERVICE}->{WRAPPER}; |
116
|
3
|
100
|
|
|
|
12
|
if ($when eq 'outer') { |
|
|
100
|
|
|
|
|
|
117
|
1
|
|
|
|
|
6
|
return shift @$wrappers; |
118
|
|
|
|
|
|
|
} elsif ($when eq 'inner') { |
119
|
1
|
|
|
|
|
6
|
return pop @$wrappers; |
120
|
|
|
|
|
|
|
} else { |
121
|
1
|
|
|
|
|
5
|
raise core_template => "'$when' isn't a valid identifier"; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub render { |
126
|
14
|
|
|
14
|
1
|
1982
|
my ($self, $template, $tokens) = @_; |
127
|
|
|
|
|
|
|
|
128
|
14
|
100
|
|
|
|
37
|
$self->view_exists($template) or raise core_template => "'$template' doesn't exist or not a regular file"; |
129
|
|
|
|
|
|
|
|
130
|
9
|
|
|
|
|
19
|
my $content = ""; |
131
|
9
|
|
50
|
|
|
25
|
my $charset = setting('charset') || ''; |
132
|
9
|
50
|
|
|
|
27
|
my @options = length($charset) ? ( binmode => ":encoding($charset)" ) : (); |
133
|
9
|
50
|
|
|
|
30
|
$_engine->process($template, $tokens, \$content, @options) or raise core_template => $_engine->error; |
134
|
9
|
|
|
|
|
53539
|
return $content; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
sub view_exists { |
138
|
14
|
|
|
14
|
1
|
27
|
my ($self, $view) = @_; |
139
|
|
|
|
|
|
|
|
140
|
14
|
100
|
|
|
|
40
|
return 1 if ref $view; |
141
|
|
|
|
|
|
|
|
142
|
6
|
50
|
|
|
|
22
|
if ($self->config->{embedded_templates}) { |
143
|
0
|
|
|
|
|
0
|
eval { |
144
|
0
|
|
|
|
|
0
|
$_engine->context->template($view); |
145
|
|
|
|
|
|
|
}; |
146
|
0
|
|
|
|
|
0
|
return ! $@; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
6
|
|
|
|
|
186
|
return -f $view; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub view { |
153
|
0
|
|
|
0
|
1
|
|
my ($self, $view) = @_; |
154
|
|
|
|
|
|
|
|
155
|
0
|
0
|
|
|
|
|
if ($self->config->{embedded_templates}) { |
156
|
0
|
|
|
|
|
|
return $view; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
else { |
159
|
0
|
|
|
|
|
|
$self->SUPER::view($view); |
160
|
|
|
|
|
|
|
} |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
__END__ |