| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# ABSTRACT: Template toolkit engine for Dancer2 |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Dancer2::Template::TemplateToolkit; |
|
4
|
|
|
|
|
|
|
$Dancer2::Template::TemplateToolkit::VERSION = '2.0.1'; |
|
5
|
11
|
|
|
11
|
|
606969
|
use Moo; |
|
|
11
|
|
|
|
|
10174
|
|
|
|
11
|
|
|
|
|
1093
|
|
|
6
|
11
|
|
|
11
|
|
6515
|
use Carp qw; |
|
|
11
|
|
|
|
|
30
|
|
|
|
11
|
|
|
|
|
886
|
|
|
7
|
11
|
|
|
11
|
|
973
|
use Dancer2::Core::Types; |
|
|
11
|
|
|
|
|
42
|
|
|
|
11
|
|
|
|
|
184
|
|
|
8
|
11
|
|
|
11
|
|
176500
|
use Dancer2::FileUtils qw; |
|
|
11
|
|
|
|
|
38
|
|
|
|
11
|
|
|
|
|
867
|
|
|
9
|
11
|
|
|
11
|
|
84
|
use Scalar::Util (); |
|
|
11
|
|
|
|
|
22
|
|
|
|
11
|
|
|
|
|
265
|
|
|
10
|
11
|
|
|
11
|
|
4159
|
use Template; |
|
|
11
|
|
|
|
|
138721
|
|
|
|
11
|
|
|
|
|
8242
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# Override to use a different Template::Toolkit base class |
|
13
|
|
|
|
|
|
|
has 'template_class' => ( is => 'ro', default => 'Template' ); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
with 'Dancer2::Core::Role::Template'; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has '+engine' => ( isa => InstanceOf ['Template'], ); |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub _build_engine { |
|
20
|
9
|
|
|
9
|
|
113
|
my $self = shift; |
|
21
|
9
|
|
|
|
|
68
|
my $charset = $self->charset; |
|
22
|
|
|
|
|
|
|
my %tt_config = ( |
|
23
|
|
|
|
|
|
|
ANYCASE => 1, |
|
24
|
|
|
|
|
|
|
ABSOLUTE => 1, |
|
25
|
|
|
|
|
|
|
length($charset) ? ( ENCODING => $charset ) : (), |
|
26
|
9
|
50
|
|
|
|
52
|
%{ $self->config }, |
|
|
9
|
|
|
|
|
84
|
|
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
9
|
|
|
|
|
57
|
my $start_tag = $self->config->{'start_tag'}; |
|
30
|
9
|
|
33
|
|
|
94
|
my $stop_tag = $self->config->{'stop_tag'} || $self->config->{end_tag}; |
|
31
|
9
|
50
|
33
|
|
|
42
|
$tt_config{'START_TAG'} = $start_tag |
|
32
|
|
|
|
|
|
|
if defined $start_tag && $start_tag ne '[%'; |
|
33
|
9
|
50
|
33
|
|
|
38
|
$tt_config{'END_TAG'} = $stop_tag |
|
34
|
|
|
|
|
|
|
if defined $stop_tag && $stop_tag ne '%]'; |
|
35
|
|
|
|
|
|
|
|
|
36
|
9
|
|
|
|
|
23
|
Scalar::Util::weaken( my $ttt = $self ); |
|
37
|
9
|
|
|
|
|
31
|
my $include_path = $self->config->{include_path}; |
|
38
|
|
|
|
|
|
|
$tt_config{'INCLUDE_PATH'} ||= [ |
|
39
|
|
|
|
|
|
|
( defined $include_path ? $include_path : () ), |
|
40
|
27
|
|
|
27
|
|
8878
|
sub { [ $ttt->views ] }, |
|
41
|
9
|
50
|
50
|
|
|
117
|
]; |
|
42
|
|
|
|
|
|
|
|
|
43
|
9
|
|
|
|
|
184
|
my $tt = $self->template_class->new(%tt_config); |
|
44
|
9
|
50
|
|
|
|
170344
|
$Template::Stash::PRIVATE = undef if $self->config->{show_private_variables}; |
|
45
|
9
|
|
|
|
|
372
|
return $tt; |
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub render { |
|
49
|
20
|
|
|
20
|
1
|
57
|
my ( $self, $template, $tokens ) = @_; |
|
50
|
|
|
|
|
|
|
|
|
51
|
20
|
|
|
|
|
46
|
my $content = ''; |
|
52
|
20
|
|
|
|
|
127
|
my $charset = $self->charset; |
|
53
|
20
|
50
|
|
|
|
123
|
my @options = length($charset) ? ( binmode => ":encoding($charset)" ) : (); |
|
54
|
20
|
100
|
|
|
|
406
|
$self->engine->process( $template, $tokens, \$content, @options ) |
|
55
|
|
|
|
|
|
|
or croak 'Failed to render template: ' . $self->engine->error; |
|
56
|
|
|
|
|
|
|
|
|
57
|
19
|
|
|
|
|
130418
|
return $content; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Override *_pathname methods from Dancer2::Core::Role::Template |
|
61
|
|
|
|
|
|
|
# Let TT2 do the concatenation of paths to template names. |
|
62
|
|
|
|
|
|
|
# |
|
63
|
|
|
|
|
|
|
# TT2 will look in a its INCLUDE_PATH for templates. |
|
64
|
|
|
|
|
|
|
# Typically $self->views is an absolute path, and we set ABSOLUTE=> 1 above. |
|
65
|
|
|
|
|
|
|
# In that case TT2 does NOT iterate through what is set for INCLUDE_PATH |
|
66
|
|
|
|
|
|
|
# However, if its not absolute, we want to allow TT2 iterate through the |
|
67
|
|
|
|
|
|
|
# its INCLUDE_PATH, which we set to be $self->views. |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub view_pathname { |
|
70
|
24
|
|
|
24
|
0
|
1827
|
my ( $self, $view ) = @_; |
|
71
|
24
|
|
|
|
|
109
|
return $self->_template_name($view); |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub layout_pathname { |
|
75
|
4
|
|
|
4
|
0
|
11
|
my ( $self, $layout ) = @_; |
|
76
|
4
|
|
|
|
|
81
|
return path( |
|
77
|
|
|
|
|
|
|
$self->layout_dir, |
|
78
|
|
|
|
|
|
|
$self->_template_name($layout), |
|
79
|
|
|
|
|
|
|
); |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub pathname_exists { |
|
83
|
7
|
|
|
7
|
0
|
17
|
my ( $self, $pathname ) = @_; |
|
84
|
7
|
|
|
|
|
14
|
my $exists = eval { |
|
85
|
|
|
|
|
|
|
# dies if pathname can not be found via TT2's INCLUDE_PATH search |
|
86
|
7
|
|
|
|
|
137
|
$self->engine->service->context->template( $pathname ); |
|
87
|
3
|
|
|
|
|
67142
|
1; |
|
88
|
|
|
|
|
|
|
}; |
|
89
|
7
|
100
|
|
|
|
574
|
$self->log_cb->( debug => $@ ) if ! $exists; |
|
90
|
7
|
|
|
|
|
59
|
return $exists; |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |