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