| 1 | package Dancer::Template::TemplateToolkit; | ||||||||||||||||||||||||||||||||||||||
| 2 | 4 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||||||||||||||||||||
| 3 | #ABSTRACT: Template Toolkit wrapper for Dancer | ||||||||||||||||||||||||||||||||||||||
| 4 | 4 | $Dancer::Template::TemplateToolkit::VERSION = '1.3520'; | |||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use Dancer::Config 'setting'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Dancer::ModuleLoader; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use base 'Dancer::Template::Abstract'; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||
| 14 | 4 | my $_engine; | |||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||
| 16 | sub init { | ||||||||||||||||||||||||||||||||||||||
| 17 | 7 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=24 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | 7 | ▶ | my $class = $self->config->{subclass} || "Template"; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->config->{'subclass'} || 'Template'
MC/DC0%0/1
$self->config->{'subclass'} || 'Template'
$self->config->{'subclass'}
Truth table50%1/2
A
A= $self->config->{'subclass'}
| |||||||||||||||||||||||||||||||||||||||
| 20 | 7 | ▶ | raise core_template => "$class is needed by Dancer::Template::TemplateToolkit" | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$class->can("process") or "Dancer::ModuleLoader"->load($class)
MC/DC100%2/2
$class->can("process") or "Dancer::ModuleLoader"->load($class)
$class->can("process")
"Dancer::ModuleLoader"->load($class)
Truth table100%3/3
A or B
A= $class->can("process")B= "Dancer::ModuleLoader"->load($class)
| |||||||||||||||||||||||||||||||||||||||
| 21 | if !$class->can("process") and !Dancer::ModuleLoader->load($class); | ||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | my $charset = setting('charset') || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
setting('charset') || ''
MC/DC0%0/1
setting('charset') || ''
setting('charset')
Truth table50%1/2
A
A= setting('charset')
| |||||||||||||||||||||||||||||||||||||||
| 24 | 6 | ▶ | my @encoding = length($charset) ? ( ENCODING => $charset ) : (); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||
| 26 | 6 | my $is_subclass = $class ne 'Template'; | |||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||
| 28 | 6 | ▶ | my @anycase = $is_subclass ? () : ( ANYCASE => 1 ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 29 | 6 | ▶ | my @absolute = $is_subclass ? () : ( ABSOLUTE => 1 ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||
| 31 | my @inc_path = $is_subclass ? () | ||||||||||||||||||||||||||||||||||||||
| 32 | 6 | ▶ | : ( INCLUDE_PATH => $self->config->{INCLUDE_PATH} || setting('views') ); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->config->{"INCLUDE_PATH"} or setting("views")
MC/DC50%1/2
$self->config->{"INCLUDE_PATH"} or setting("views")
$self->config->{"INCLUDE_PATH"}
setting("views")
Truth table66%2/3
A or B
A= $self->config->{"INCLUDE_PATH"}B= setting("views")
| |||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||
| 34 | my $start_tag = $is_subclass | ||||||||||||||||||||||||||||||||||||||
| 35 | ? $self->config->{start_tag} | ||||||||||||||||||||||||||||||||||||||
| 36 | 6 | ▶ | : $self->config->{start_tag} || '<%'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->config->{"start_tag"} or "<%"
MC/DC0%0/1
$self->config->{"start_tag"} or "<%"
$self->config->{"start_tag"}
Truth table50%1/2
A
A= $self->config->{"start_tag"}
| |||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||
| 38 | my $stop_tag = $is_subclass | ||||||||||||||||||||||||||||||||||||||
| 39 | ? $self->config->{stop_tag} || $self->config->{end_tag} | ||||||||||||||||||||||||||||||||||||||
| 40 | 6 | ▶ | : $self->config->{stop_tag} || $self->config->{end_tag} || '%>'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%2/8
$self->config->{"stop_tag"} or $self->config->{"end_tag"}
$self->config->{"stop_tag"} or $self->config->{"end_tag"} or "%>"
$self->config->{"stop_tag"} or $self->config->{"end_tag"}
MC/DC0%0/2
$self->config->{"stop_tag"} or $self->config->{"end_tag"} or "%>"
$self->config->{"stop_tag"}
$self->config->{"end_tag"}
Truth table33%1/3
A or B
A= $self->config->{"stop_tag"}B= $self->config->{"end_tag"}
| |||||||||||||||||||||||||||||||||||||||
| 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 | my @start = (); | |||||||||||||||||||||||||||||||||||||
| 49 | 6 | ▶ | if (defined $start_tag) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 50 | 6 | ▶ | @start = ( START_TAG => $start_tag eq '\[%' || $start_tag eq '\[\%' | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$start_tag eq "\\[%" or $start_tag eq "\\[\\%"
MC/DC0%0/2
$start_tag eq "\\[%" or $start_tag eq "\\[\\%"
$start_tag eq "\\[%"
$start_tag eq "\\[\\%"
Truth table33%1/3
A or B
A= $start_tag eq "\\[%"B= $start_tag eq "\\[\\%"
| |||||||||||||||||||||||||||||||||||||||
| 51 | ? $start_tag | ||||||||||||||||||||||||||||||||||||||
| 52 | : quotemeta($start_tag) | ||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||
| 55 | 6 | my @stop = (); | |||||||||||||||||||||||||||||||||||||
| 56 | 6 | ▶ | if (defined $stop_tag) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 57 | 6 | ▶ | @stop = ( END_TAG => $stop_tag eq '%\]' || $stop_tag eq '\%\]' | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$stop_tag eq "%\\]" or $stop_tag eq "\\%\\]"
MC/DC0%0/2
$stop_tag eq "%\\]" or $stop_tag eq "\\%\\]"
$stop_tag eq "%\\]"
$stop_tag eq "\\%\\]"
Truth table33%1/3
A or B
A= $stop_tag eq "%\\]"B= $stop_tag eq "\\%\\]"
| |||||||||||||||||||||||||||||||||||||||
| 58 | ? $stop_tag | ||||||||||||||||||||||||||||||||||||||
| 59 | : quotemeta($stop_tag) | ||||||||||||||||||||||||||||||||||||||
| 60 | ); | ||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||
| 62 | 6 | my @embedded = (); | |||||||||||||||||||||||||||||||||||||
| 63 | 6 | ▶ | if ($self->config->{embedded_templates}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | Dancer::ModuleLoader->load('Template::Provider::FromDATA') | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 65 | or croak "The Package Template::Provider::FromDATA must be installed to use embedded_templates"; | ||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||
| 67 | 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 | %{$self->config}, | |||||||||||||||||||||||||||||||||||||
| 79 | }; | ||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||
| 81 | 6 | $_engine = $class->new(%$tt_config); | |||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||
| 84 | sub set_wrapper { | ||||||||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | my ($self, $when, $file) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
set_wrapper: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 86 | 4 | my $wrappers = $_engine->{SERVICE}->{WRAPPER}; | |||||||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | unless (defined $file) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 88 | 1 | $file = $when; | |||||||||||||||||||||||||||||||||||||
| 89 | 1 | my @orig = @$wrappers; | |||||||||||||||||||||||||||||||||||||
| 90 | 1 | $self->{orig_wrappers} = \@orig; | |||||||||||||||||||||||||||||||||||||
| 91 | 1 | @$wrappers = ($file); | |||||||||||||||||||||||||||||||||||||
| 92 | 1 | return; | |||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||
| 94 | 3 | ▶ | if ($when eq 'outer') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 95 | 1 | unshift @$wrappers => $file; | |||||||||||||||||||||||||||||||||||||
| 96 | } elsif ($when eq 'inner') { | ||||||||||||||||||||||||||||||||||||||
| 97 | 1 | push @$wrappers => $file; | |||||||||||||||||||||||||||||||||||||
| 98 | } else { | ||||||||||||||||||||||||||||||||||||||
| 99 | 1 | raise core_template => "'$when' isn't a valid identifier"; | |||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | sub reset_wrapper { | ||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_wrapper: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 105 | 1 | my $wrappers = $_engine->{SERVICE}->{WRAPPER}; | |||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | my $orig = $self->{orig_wrappers} || []; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'orig_wrappers'} || []
MC/DC0%0/1
$self->{'orig_wrappers'} || []
$self->{'orig_wrappers'}
Truth table50%1/2
A
A= $self->{'orig_wrappers'}
| |||||||||||||||||||||||||||||||||||||||
| 107 | 1 | my @old = @$wrappers; | |||||||||||||||||||||||||||||||||||||
| 108 | 1 | @$wrappers = @$orig; | |||||||||||||||||||||||||||||||||||||
| 109 | 1 | return @old; | |||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||
| 112 | sub unset_wrapper { | ||||||||||||||||||||||||||||||||||||||
| 113 | 3 | ▶ | my ($self, $when) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
unset_wrapper: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 114 | 3 | my $wrappers = $_engine->{SERVICE}->{WRAPPER}; | |||||||||||||||||||||||||||||||||||||
| 115 | 3 | ▶ | if ($when eq 'outer') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 116 | 1 | return shift @$wrappers; | |||||||||||||||||||||||||||||||||||||
| 117 | } elsif ($when eq 'inner') { | ||||||||||||||||||||||||||||||||||||||
| 118 | 1 | return pop @$wrappers; | |||||||||||||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||||||||||||
| 120 | 1 | raise core_template => "'$when' isn't a valid identifier"; | |||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||
| 124 | sub render { | ||||||||||||||||||||||||||||||||||||||
| 125 | 14 | ▶ | my ($self, $template, $tokens) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
render: called CC=5 SCAR=16.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||
| 127 | 14 | ▶ | $self->view_exists($template) or raise core_template => "'$template' doesn't exist or not a regular file"; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||
| 129 | 9 | my $content = ""; | |||||||||||||||||||||||||||||||||||||
| 130 | 9 | ▶ | my $charset = setting('charset') || ''; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
setting('charset') || ''
MC/DC0%0/1
setting('charset') || ''
setting('charset')
Truth table50%1/2
A
A= setting('charset')
| |||||||||||||||||||||||||||||||||||||||
| 131 | 9 | ▶ | my @options = length($charset) ? ( binmode => ":encoding($charset)" ) : (); | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 132 | 9 | ▶ | $_engine->process($template, $tokens, \$content, @options) or raise core_template => $_engine->error; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 133 | 9 | return $content; | |||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||
| 136 | sub view_exists { | ||||||||||||||||||||||||||||||||||||||
| 137 | 14 | ▶ | my ($self, $view) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
view_exists: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||
| 139 | 14 | ▶ | return 1 if ref $view; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||
| 141 | 6 | ▶ | if ($self->config->{embedded_templates}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 142 | 0 | eval { | |||||||||||||||||||||||||||||||||||||
| 143 | 0 | $_engine->context->template($view); | |||||||||||||||||||||||||||||||||||||
| 144 | }; | ||||||||||||||||||||||||||||||||||||||
| 145 | 0 | return ! $@; | |||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||
| 148 | 6 | return -f $view; | |||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||
| 151 | sub view { | ||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my ($self, $view) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
view: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||
| 154 | 0 | ▶ | if ($self->config->{embedded_templates}) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 155 | 0 | return $view; | |||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||||||||||||
| 158 | 0 | $self->SUPER::view($view); | |||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||
| 162 | 4 | 1; | |||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||||||||||||||||||||