| 1 | # ABSTRACT: Template toolkit engine for Dancer2 | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Dancer2::Template::TemplateToolkit; | ||||||||||||||||||||||||||||
| 4 | 15 | $Dancer2::Template::TemplateToolkit::VERSION = '2.3.0'; | |||||||||||||||||||||||||||
| 5 | 15 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use Carp qw<croak>; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Dancer2::Core::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Path::Tiny (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # Override to use a different Template::Toolkit base class | ||||||||||||||||||||||||||||
| 13 | 15 | has 'template_class' => ( is => 'ro', default => 'Template' ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 15 | with 'Dancer2::Core::Role::Template'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 15 | has '+engine' => ( isa => InstanceOf ['Template'], ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 18 | ▶ | sub _build_engine { | ||||||||||||||||||||||||||
|
Subroutine
_build_engine: called CC=10 SCAR=26.2
| |||||||||||||||||||||||||||||
| 20 | 18 | my $self = shift; | |||||||||||||||||||||||||||
| 21 | 18 | my $charset = $self->charset; | |||||||||||||||||||||||||||
| 22 | my %tt_config = ( | ||||||||||||||||||||||||||||
| 23 | ANYCASE => 1, | ||||||||||||||||||||||||||||
| 24 | ABSOLUTE => 1, | ||||||||||||||||||||||||||||
| 25 | length($charset) ? ( ENCODING => $charset ) : (), | ||||||||||||||||||||||||||||
| 26 | 18 | ▶ | %{ $self->config }, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 18 | my $start_tag = $self->config->{'start_tag'}; | |||||||||||||||||||||||||||
| 30 | 18 | ▶ | my $stop_tag = $self->config->{'stop_tag'} || $self->config->{end_tag}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->config->{'stop_tag'} || $self->config->{'end_tag'}
MC/DC0%0/2
$self->config->{'stop_tag'} || $self->config->{'end_tag'}
$self->config->{'stop_tag'}
$self->config->{'end_tag'}
Truth table33%1/3
A || B
A= $self->config->{'stop_tag'}B= $self->config->{'end_tag'}
| |||||||||||||||||||||||||||||
| 31 | 18 | ▶ | $tt_config{'START_TAG'} = $start_tag | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $start_tag and $start_tag ne "[%"
MC/DC0%0/2
defined $start_tag and $start_tag ne "[%"
defined $start_tag
$start_tag ne "[%"
Truth table33%1/3
A and B
A= defined $start_tagB= $start_tag ne "[%"
| |||||||||||||||||||||||||||||
| 32 | if defined $start_tag && $start_tag ne '[%'; | ||||||||||||||||||||||||||||
| 33 | 18 | ▶ | $tt_config{'END_TAG'} = $stop_tag | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $stop_tag and $stop_tag ne "%]"
MC/DC0%0/2
defined $stop_tag and $stop_tag ne "%]"
defined $stop_tag
$stop_tag ne "%]"
Truth table33%1/3
A and B
A= defined $stop_tagB= $stop_tag ne "%]"
| |||||||||||||||||||||||||||||
| 34 | if defined $stop_tag && $stop_tag ne '%]'; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 18 | Scalar::Util::weaken( my $ttt = $self ); | |||||||||||||||||||||||||||
| 37 | 18 | my $include_path = $self->config->{include_path}; | |||||||||||||||||||||||||||
| 38 | $tt_config{'INCLUDE_PATH'} ||= [ | ||||||||||||||||||||||||||||
| 39 | ( defined $include_path ? $include_path : () ), | ||||||||||||||||||||||||||||
| 40 | 66 | ▶ | sub { [ $ttt->views ] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 18 | ▶ | ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$tt_config{"INCLUDE_PATH"} ||= [defined $include_path ? $include_path : (), sub {
[$ttt->views];
}
]
MC/DC0%0/1
$tt_config{"INCLUDE_PATH"} ||= [defined $include_path ? $include_path : (), sub {
[$ttt->views];
}
]
$tt_config{"INCLUDE_PATH"}
Truth table50%1/2
A
A= $tt_config{"INCLUDE_PATH"}
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 18 | my $tt = $self->template_class->new(%tt_config); | |||||||||||||||||||||||||||
| 44 | 18 | ▶ | $Template::Stash::PRIVATE = undef if $self->config->{show_private_variables}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 18 | return $tt; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 43 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 43 | my ( $self, $template, $tokens ) = @_; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 43 | my $content = ''; | |||||||||||||||||||||||||||
| 52 | 43 | my $charset = $self->charset; | |||||||||||||||||||||||||||
| 53 | 43 | ▶ | my @options = length($charset) ? ( binmode => ":encoding($charset)" ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 43 | ▶ | $self->engine->process( $template, $tokens, \$content, @options ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | or croak 'Failed to render template: ' . $self->engine->error; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 39 | 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 | 61 | ▶ | sub view_pathname { | ||||||||||||||||||||||||||
|
Subroutine
view_pathname: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 61 | my ( $self, $view ) = @_; | |||||||||||||||||||||||||||
| 71 | 61 | return $self->_template_name($view); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 9 | ▶ | sub layout_pathname { | ||||||||||||||||||||||||||
|
Subroutine
layout_pathname: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 9 | my ( $self, $layout ) = @_; | |||||||||||||||||||||||||||
| 76 | 9 | return Path::Tiny::path( | |||||||||||||||||||||||||||
| 77 | $self->layout_dir, | ||||||||||||||||||||||||||||
| 78 | $self->_template_name($layout), | ||||||||||||||||||||||||||||
| 79 | )->stringify; | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 26 | ▶ | sub pathname_exists { | ||||||||||||||||||||||||||
|
Subroutine
pathname_exists: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 26 | my ( $self, $pathname ) = @_; | |||||||||||||||||||||||||||
| 84 | 26 | my $exists = eval { | |||||||||||||||||||||||||||
| 85 | # dies if pathname can not be found via TT2's INCLUDE_PATH search | ||||||||||||||||||||||||||||
| 86 | 26 | $self->engine->service->context->template( $pathname ); | |||||||||||||||||||||||||||
| 87 | 8 | 1; | |||||||||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||||||||||
| 89 | 26 | ▶ | $self->log_cb->( debug => $@ ) if ! $exists; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 26 | return $exists; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 15 | 1; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||||||||||||