File Coverage

blib/lib/Dancer2/Template/TemplateToolkit.pm
Criterion Covered Total %
statement 49 49 100.0
branch 10 16 62.5
condition 4 11 36.3
subroutine 12 12 100.0
pod 1 4 25.0
total 76 92 82.6


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.1.0';
5 11     11   648970 use Moo;
  11         11097  
  11         1103  
6 11     11   8217 use Carp qw;
  11         27  
  11         923  
7 11     11   972 use Dancer2::Core::Types;
  11         30  
  11         211  
8 11     11   180249 use Path::Tiny ();
  11         49101  
  11         287  
9 11     11   74 use Scalar::Util ();
  11         46  
  11         310  
10 11     11   4484 use Template;
  11         149776  
  11         8011  
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   130 my $self = shift;
21 9         42 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         114  
27             );
28              
29 9         46 my $start_tag = $self->config->{'start_tag'};
30 9   33     106 my $stop_tag = $self->config->{'stop_tag'} || $self->config->{end_tag};
31 9 50 33     49 $tt_config{'START_TAG'} = $start_tag
32             if defined $start_tag && $start_tag ne '[%';
33 9 50 33     44 $tt_config{'END_TAG'} = $stop_tag
34             if defined $stop_tag && $stop_tag ne '%]';
35              
36 9         27 Scalar::Util::weaken( my $ttt = $self );
37 9         40 my $include_path = $self->config->{include_path};
38             $tt_config{'INCLUDE_PATH'} ||= [
39             ( defined $include_path ? $include_path : () ),
40 27     27   9885 sub { [ $ttt->views ] },
41 9 50 50     145 ];
42              
43 9         209 my $tt = $self->template_class->new(%tt_config);
44 9 50       192639 $Template::Stash::PRIVATE = undef if $self->config->{show_private_variables};
45 9         475 return $tt;
46             }
47              
48             sub render {
49 20     20 1 70 my ( $self, $template, $tokens ) = @_;
50              
51 20         46 my $content = '';
52 20         122 my $charset = $self->charset;
53 20 50       148 my @options = length($charset) ? ( binmode => ":encoding($charset)" ) : ();
54 20 100       496 $self->engine->process( $template, $tokens, \$content, @options )
55             or croak 'Failed to render template: ' . $self->engine->error;
56              
57 19         167473 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 1301 my ( $self, $view ) = @_;
71 24         156 return $self->_template_name($view);
72             }
73              
74             sub layout_pathname {
75 4     4 0 16 my ( $self, $layout ) = @_;
76 4         86 return Path::Tiny::path(
77             $self->layout_dir,
78             $self->_template_name($layout),
79             )->stringify;
80             }
81              
82             sub pathname_exists {
83 7     7 0 26 my ( $self, $pathname ) = @_;
84 7         17 my $exists = eval {
85             # dies if pathname can not be found via TT2's INCLUDE_PATH search
86 7         197 $self->engine->service->context->template( $pathname );
87 3         71453 1;
88             };
89 7 100       944 $self->log_cb->( debug => $@ ) if ! $exists;
90 7         90 return $exists;
91             }
92              
93             1;
94              
95             __END__