| 1 | package Dancer::Template::Abstract; | ||||||||||||||||||||
| 2 | 245 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||
| 3 | #ABSTRACT: abstract class for Dancer's template engines | ||||||||||||||||||||
| 4 | 245 | $Dancer::Template::Abstract::VERSION = '1.3522'; | |||||||||||||||||||
| 5 | 245 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 245 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 245 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 245 | ▶ | use Dancer::Logger; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 245 | ▶ | use Dancer::Factory::Hook; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 245 | ▶ | use Dancer::FileUtils 'path'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 245 | ▶ | use Dancer::Exception qw(:all); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 245 | ▶ | use base 'Dancer::Engine'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 245 | Dancer::Factory::Hook->instance->install_hooks( | |||||||||||||||||||
| 17 | qw/before_template_render after_template_render before_layout_render after_layout_render/ | ||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # overloads this method to implement the rendering | ||||||||||||||||||||
| 21 | # args: $self, $template, $tokens | ||||||||||||||||||||
| 22 | # return: a string of $template's content processed with $tokens | ||||||||||||||||||||
| 23 | 1 | ▶ | sub render { confess "render not implemented" } | ||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 110 | ▶ | sub default_tmpl_ext { "tt" } | ||||||||||||||||||
|
Subroutine
default_tmpl_ext: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | # Work out the template names to look for; this will be the view name passed | ||||||||||||||||||||
| 28 | # as-is, and also the view name with the default template extension appended, if | ||||||||||||||||||||
| 29 | # it did not already end in that. | ||||||||||||||||||||
| 30 | 112 | ▶ | sub _template_name { | ||||||||||||||||||
|
Subroutine
_template_name: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 31 | 112 | my ( $self, $view ) = @_; | |||||||||||||||||||
| 32 | 112 | my @views = ( $view ); | |||||||||||||||||||
| 33 | 112 | ▶ | my $def_tmpl_ext = $self->config->{extension} || $self->default_tmpl_ext(); | ||||||||||||||||||
|
Condition66%2/3
$self->config->{'extension'} || $self->default_tmpl_ext
MC/DC0%0/2
$self->config->{'extension'} || $self->default_tmpl_ext
$self->config->{'extension'}
$self->default_tmpl_ext
Truth table66%2/3
A || B
A= $self->config->{'extension'}B= $self->default_tmpl_ext
| |||||||||||||||||||||
| 34 | 112 | ▶ | push @views, $view .= ".$def_tmpl_ext" if $view !~ /\.\Q$def_tmpl_ext\E$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 112 | return @views; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 106 | ▶ | sub view { | ||||||||||||||||||
|
Subroutine
view: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 39 | 106 | my ($self, $view) = @_; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 106 | my $views_dir = Dancer::App->current->setting('views'); | |||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 106 | for my $template ($self->_template_name($view)) { | |||||||||||||||||||
| 44 | 194 | my $view_path = path($views_dir, $template); | |||||||||||||||||||
| 45 | 194 | ▶ | return $view_path if -f $view_path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | # No matching view path was found | ||||||||||||||||||||
| 49 | 5 | return; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 5 | ▶ | sub layout { | ||||||||||||||||||
|
Subroutine
layout: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||
| 53 | 5 | my ($self, $layout, $tokens, $content) = @_; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 5 | my $layouts_dir = path(Dancer::App->current->setting('views'), 'layouts'); | |||||||||||||||||||
| 56 | 5 | my $layout_path; | |||||||||||||||||||
| 57 | 5 | for my $layout_name ($self->_template_name($layout)) { | |||||||||||||||||||
| 58 | 10 | $layout_path = path($layouts_dir, $layout_name); | |||||||||||||||||||
| 59 | 10 | ▶ | last if -e $layout_path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 5 | my $full_content; | |||||||||||||||||||
| 63 | 5 | ▶ | if (-e $layout_path) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 5 | $full_content = Dancer::Template->engine->render( | |||||||||||||||||||
| 65 | $layout_path, {%$tokens, content => $content}); | ||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||
| 67 | 0 | $full_content = $content; | |||||||||||||||||||
| 68 | 0 | Dancer::Logger::error("Defined layout ($layout) was not found!"); | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | 5 | $full_content; | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 56 | ▶ | sub apply_renderer { | ||||||||||||||||||
|
Subroutine
apply_renderer: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||
| 74 | 56 | my ($self, $view, $tokens) = @_; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 56 | ($tokens, undef) = _prepare_tokens_options($tokens); | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 56 | $view = $self->view($view); | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 56 | Dancer::Factory::Hook->execute_hooks('before_template_render', $tokens); | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 45 | my $content; | |||||||||||||||||||
| 83 | 45 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 84 | 45 | $content = $self->render($view, $tokens); | |||||||||||||||||||
| 85 | 0 | ▶ | } continuation { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 86 | 0 | my ($continuation) = @_; | |||||||||||||||||||
| 87 | # If we have a Route continuation, run the after hook, then | ||||||||||||||||||||
| 88 | # propagate the continuation | ||||||||||||||||||||
| 89 | 0 | Dancer::Factory::Hook->execute_hooks('after_template_render', \$content); | |||||||||||||||||||
| 90 | 0 | $continuation->rethrow(); | |||||||||||||||||||
| 91 | 45 | }; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 45 | Dancer::Factory::Hook->execute_hooks('after_template_render', \$content); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | # make sure to avoid ( undef ) in list context return | ||||||||||||||||||||
| 96 | 42 | ▶ | defined $content | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | and return $content; | ||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 27 | ▶ | sub apply_layout { | ||||||||||||||||||
|
Subroutine
apply_layout: called CC=6 SCAR=18.7
Pod
undocumented
| |||||||||||||||||||||
| 102 | 27 | my ($self, $content, $tokens, $options) = @_; | |||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 27 | ($tokens, $options) = _prepare_tokens_options($tokens, $options); | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | # If 'layout' was given in the options hashref, use it if it's a true value, | ||||||||||||||||||||
| 107 | # or don't use a layout if it was false (0, or undef); if layout wasn't | ||||||||||||||||||||
| 108 | # given in the options hashref, go with whatever the current layout setting | ||||||||||||||||||||
| 109 | # is. | ||||||||||||||||||||
| 110 | my $layout = | ||||||||||||||||||||
| 111 | exists $options->{layout} | ||||||||||||||||||||
| 112 | ? ($options->{layout} ? $options->{layout} : undef) | ||||||||||||||||||||
| 113 | 27 | ▶ | : Dancer::App->current->setting('layout'); | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 27 | ▶ | defined $content or return; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 27 | ▶ | defined $layout or return $content; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 5 | Dancer::Factory::Hook->execute_hooks('before_layout_render', $tokens, \$content); | |||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 5 | my $full_content; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | 5 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 124 | 5 | $full_content = $self->layout($layout, $tokens, $content); | |||||||||||||||||||
| 125 | 0 | ▶ | } continuation { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 126 | 0 | my ($continuation) = @_; | |||||||||||||||||||
| 127 | # If we have a Route continuation, run the after hook, then | ||||||||||||||||||||
| 128 | # propagate the continuation | ||||||||||||||||||||
| 129 | 0 | Dancer::Factory::Hook->execute_hooks('after_layout_render', \$full_content); | |||||||||||||||||||
| 130 | 0 | $continuation->rethrow(); | |||||||||||||||||||
| 131 | 5 | }; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 5 | Dancer::Factory::Hook->execute_hooks('after_layout_render', \$full_content); | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | # make sure to avoid ( undef ) in list context return | ||||||||||||||||||||
| 136 | 5 | ▶ | defined $full_content | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | and return $full_content; | ||||||||||||||||||||
| 138 | 0 | return; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 83 | ▶ | sub _prepare_tokens_options { | ||||||||||||||||||
|
Subroutine
_prepare_tokens_options: called CC=5 SCAR=16.1
| |||||||||||||||||||||
| 142 | 83 | my ($tokens, $options) = @_; | |||||||||||||||||||
| 143 | |||||||||||||||||||||
| 144 | 83 | ▶ | $options ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$options ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | # these are the default tokens provided for template processing | ||||||||||||||||||||
| 147 | 83 | ▶ | $tokens ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$tokens ||= {}
MC/DC100%1/1
$tokens ||= {}
$tokens
Truth table100%2/2
A
A= $tokens
| |||||||||||||||||||||
| 148 | 83 | $tokens->{perl_version} = $]; | |||||||||||||||||||
| 149 | 83 | $tokens->{dancer_version} = $Dancer::VERSION; | |||||||||||||||||||
| 150 | 83 | $tokens->{settings} = Dancer::Config->settings; | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | # If we're processing a request, also add the request object, params and | ||||||||||||||||||||
| 153 | # vars as tokens: | ||||||||||||||||||||
| 154 | 83 | ▶ | if (my $request = Dancer::SharedData->request) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 155 | 83 | $tokens->{request} = $request; | |||||||||||||||||||
| 156 | 83 | $tokens->{params} = $request->params; | |||||||||||||||||||
| 157 | 83 | $tokens->{vars} = Dancer::SharedData->vars; | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | Dancer::App->current->setting('session') | ||||||||||||||||||||
| 161 | 83 | ▶ | and $tokens->{session} = Dancer::Session->get; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 83 | return ($tokens, $options); | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 39 | ▶ | sub template { | ||||||||||||||||||
|
Subroutine
template: called CC=8 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||
| 167 | 39 | my ($class, $view, $tokens, $options) = @_; | |||||||||||||||||||
| 168 | 39 | my ($content, $full_content); | |||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 39 | my $engine = Dancer::Template->engine; | |||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | # it's important that $tokens is not undef, so that things added to it via | ||||||||||||||||||||
| 173 | # a before_template in apply_renderer survive to the apply_layout. GH#354 | ||||||||||||||||||||
| 174 | 39 | ▶ | $tokens ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$tokens ||= {}
MC/DC100%1/1
$tokens ||= {}
$tokens
Truth table100%2/2
A
A= $tokens
| |||||||||||||||||||||
| 175 | 39 | ▶ | $options ||= {}; | ||||||||||||||||||
|
Condition100%2/2
$options ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 39 | ▶ | if ($view) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | # check if the requested view exists | ||||||||||||||||||||
| 179 | 39 | ▶ | my $view_path = $engine->view($view) || ''; | ||||||||||||||||||
|
Condition100%2/2
$engine->view($view) || ''
MC/DC100%1/1
$engine->view($view) || ''
$engine->view($view)
Truth table100%2/2
A
A= $engine->view($view)
| |||||||||||||||||||||
| 180 | 39 | ▶ | if ($engine->view_exists($view_path)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 181 | 38 | $content = $engine->apply_renderer($view, $tokens); | |||||||||||||||||||
| 182 | } else { | ||||||||||||||||||||
| 183 | 1 | Dancer::Logger::error( | |||||||||||||||||||
| 184 | "Supplied view ($view) not found - $view_path does not exist" | ||||||||||||||||||||
| 185 | ); | ||||||||||||||||||||
| 186 | 1 | return Dancer::Error->new( | |||||||||||||||||||
| 187 | code => 500, | ||||||||||||||||||||
| 188 | message => 'view not found', | ||||||||||||||||||||
| 189 | )->render(); | ||||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | } else { | ||||||||||||||||||||
| 192 | 0 | $content = delete $options->{content}; | |||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 24 | ▶ | defined $content and $full_content = | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 196 | $engine->apply_layout($content, $tokens, $options); | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | 24 | ▶ | defined $full_content | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 199 | and return $full_content; | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | 0 | Dancer::Error->new( | |||||||||||||||||||
| 202 | code => 404, | ||||||||||||||||||||
| 203 | message => "Page not found", | ||||||||||||||||||||
| 204 | )->render(); | ||||||||||||||||||||
| 205 | } | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 39 | ▶ | sub view_exists { return defined $_[1] && -f $_[1] } | ||||||||||||||||||
|
Subroutine
view_exists: called CC=2 SCAR=7.2
Pod
documented
Condition66%2/3
defined $_[1] && -f $_[1]
MC/DC50%1/2
defined $_[1] && -f $_[1]
defined $_[1]
-f $_[1]
Truth table66%2/3
A && B
A= defined $_[1]B= -f $_[1]
| |||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | 245 | 1; | |||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | __END__ | ||||||||||||||||||||