Hector/Response/TemplateToolkit.pm

filter: Statementstmt 58.1% 50 / 86 Branchbran 33.3% 8 / 24 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 62.5% 10 / 16 Podpod 85.7% 6 / 7 totaltotal 52.3% 77 / 147 CC 16 SCAR 27.8
CC
16
Cov
51%
_getContent
42.0
display
20.9
setError
2.0
CRAP 16.2
« Hector/Request.pm ↑ Summary Hector/Middleware.pm »
1 #response object
2 package PSGI::Hector::Response::TemplateToolkit;
3  
4 =pod
5  
6 =head1 NAME
7  
8 Response TemplateToolkit - View plugin using template toolkit
9  
10 =head1 SYNOPSIS
11  
12 my $response = $hector->getResponse();
13 $response->setTemplateVar("hello", $something);
14  
15 =head1 DESCRIPTION
16  
17 This view plugin allows you to read a template file and replace placholders with scalar variables.
18  
19 With this class you can specify empty Hector actions to just display a static page.
20  
21 The display() can additionally be called by a controller action multiple times if several different templates need to be output.
22  
23 =head1 METHODS
24  
25 =cut
26  
27 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use parent qw(PSGI::Hector::Response::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 our $templateLoc = "root/templates"; #where the templates are stored
33 #########################################################
34  
35 =head2 new($hector)
36  
37 Constructor, All environment hash is saved in template variable "env" and the current action is saved as "action" so they can be accessed
38 along with any other variables stored during the server action in the usual template toolkit way.
39  
40 =cut
41  
42 #########################################################
43 1 ▶sub new{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
44 1 my($class, $hector) = @_;
45 1 my $self = $class->SUPER::new($hector);
46 1 $self->{'_template'} = undef;
47 1 $self->{'_templateVars'} = {};
48 1 bless $self, $class;
49 1 $self->setTemplateVar("env", $hector->getEnv()); #include this var by default
50 1 $self->setTemplateVar("hector", $hector); #this will be handy to have too
51 1 $self->setTemplateVar("action", $hector->getAction()); #this will be handy to have too
52 1 $self->setTemplateVar("debug", $hector->getOption("debug"));
53 1 return $self;
54 }
55 #########################################################
56  
57 =head2 setTemplate($template)
58  
59 $response->setTemplate("login");
60  
61 Manually set the template to display.
62  
63 An file extension of '.html' will be automatically appended to this name.
64  
65 The template will be fetched from the template directory, See the L<Notes>
66 section for more details.
67  
68 =cut
69  
70 #########################################################
71 1 ▶sub setTemplate{
Subroutine setTemplate: called CC=1 SCAR=0.0
Pod documented
72 1 my($self, $template) = @_;
73 1 $self->{'_template'} = $template;
74 1 return 1;
75 }
76 #########################################################
77 4 ▶sub getTemplate{
Subroutine getTemplate: called CC=1 SCAR=0.0
Pod undocumented
78 4 my $self = shift;
79 4 return $self->{'_template'};
80 }
81 #########################################################
82  
83 =pod
84  
85 =head2 display()
86  
87 $response->display();
88  
89 This method is called automatically at the end of an action.
90  
91 A template is automatically chosen. An example demonstrates how this is done.
92  
93 URL used: /foo/bar/app.cgi?action=login
94 Template chosen: app-login.html
95  
96 =cut
97  
98 #########################################################
99 2 ▶sub display{ #this sub will display the page headers if needed
Subroutine display: called CC=11 SCAR=30.4
Pod documented
100 2 my $self = shift;
101 2 ▶ if(!$self->getTemplate()){ #if no template has been set in the action sub then we set a default
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->getTemplate) 0 2 2
102 0 my $tName = $self->_getTemplateNameForAction();
103 0 $self->setTemplate($tName); #set the template automatically
104 }
105 2 ▶ if(!$self->getError() && !$self->header("Location") && !$self->getTemplate()){ #we must have a template set if we dont have an error or a redirect
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->getError or $self->header("Location") or $self->getTemplate) 0 2 2
Condition33%2/6
$self->getError or $self->header("Location") or $self->getTemplate
l!l&&r!l&&!r
020
$self->getError or $self->header("Location")
l!l&&r!l&&!r
002
MC/DC0%0/3
$self->getError or $self->header("Location") or $self->getTemplate
$self->getError $self->header("Location") $self->getTemplate
Truth table25%1/4
A or B or C
A= $self->getErrorB= $self->header("Location")C= $self->getTemplate
ABCresult
1XX1
01X1
0011
0000
106 0 $self->setError("No template defined");
107 }
108 2 ▶ if($self->_getDisplayedHeader()){ #just display more content
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_getDisplayedHeader) { } 1 1 2
109 1 my $content = $self->_getContent(); #get the contents of the template
110 1 $self->add_content_utf8($content);
111 }
112 else{ #first output so display any headers
113 1 ▶ if($self->header("Location")){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->header('Location')) { } 0 1 1
114 0 $self->code(302);
115 0 $self->message('Found');
116 }
117 else{ #if we dont have a redirect
118 1 ▶ if(!$self->header("Content-Type")){ #set default content type
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->header("Content-Type")) 1 0 1
119 1 $self->header("Content-Type" => "text/html");
120 }
121 1 my $content = $self->_getContent(); #get the contents of the template
122 1 $self->add_content_utf8($content);
123 }
124 1 ▶ if($self->getError() && $self->code() =~ m/^[123]/){ #set the error code when needed
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getError and $self->code =~ /^[123]/) 0 1 1
Condition33%1/3
$self->getError and $self->code =~ /^[123]/
!ll&&!rl&&r
100
MC/DC0%0/2
$self->getError and $self->code =~ /^[123]/
$self->getError $self->code =~ /^[123]/
Truth table33%1/3
A and B
A= $self->getErrorB= $self->code =~ /^[123]/
ABresult
0X0
100
111
125 0 $self->code(500);
126 0 $self->message('Internal Server Error');
127 }
128 1 $self->_setDisplayedHeader(); #we wont display the header again
129 }
130 2 ▶ if($self->getError()){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getError) 0 2 2
131 0 $self->getHector()->getLog()->log($self->getError(), 'error'); #just log it so we have a record of this
132 }
133 2 return $self->SUPER::display();
134 }
135 #########################################################
136  
137 =pod
138  
139 =head2 setError($message)
140  
141 $response->setError("something has broken");
142  
143 Set an error message for the response, which is accessible in the error template
144 as [% message %].
145  
146 =cut
147  
148 #########################################################
149 0 ▶sub setError(){
Subroutine setError: not called CC=1 SCAR=6.9
Pod documented
150 0 my($self, $message) = @_;
151 0 $self->setTemplateVar("message", $message); #so we can access the error message via smarty
152 0 return $self->SUPER::setError($message); #save the message for later in the instance
153 }
154 #########################################################
155  
156 =pod
157  
158 =head2 setTemplateVar($name, $value)
159  
160 $response->setTemplatevar("name", "Bob");
161  
162 Creates a template variable with the specified name and value.
163 The variable may be of any type and can be access from the template in the
164 usual way.
165  
166 =cut
167  
168 #########################################################
169 4 ▶sub setTemplateVar{
Subroutine setTemplateVar: called CC=1 SCAR=0.0
Pod documented
170 4 my($self, $name, $value) = @_;
171 4 $self->{'_templateVars'}->{$name} = $value;
172 4 return 1;
173 }
174 #########################################################
175  
176 =pod
177  
178 =head2 getTemplateVar($name)
179  
180 $response->getTemplateVar("name");
181  
182 Returns the value of the given template variable.
183  
184 =cut
185  
186 #########################################################
187 0 ▶sub getTemplateVar{
Subroutine getTemplateVar: not called CC=1 SCAR=6.9
Pod documented
188 0 my($self, $name) = @_;
189 0 my $vars = $self->_getTemplateVars();
190 0 return $vars->{$name};
191 }
192 #########################################################
193 # private methods
194 ########################################################
195 0 ▶sub _getTemplateVars{
Subroutine _getTemplateVars: not called CC=1 SCAR=6.9
196 0 my $self = shift;
197 0 return $self->{'_templateVars'};
198 }
199 #########################################################
200 0 ▶sub _getContent{
Subroutine _getContent: not called CC=6 SCAR=37.4
201 0 my $self = shift;
202 0 my $content = "";
203 0 my $tt = Template->new(
204 {
205 INCLUDE_PATH => $self->_getTemplatePath(),
206 ENCODING => 'utf-8'
207 }
208 );
209 0 ▶ if($tt){
Branch: 1 branch
BranchTrueFalseTotal
if ($tt) { } 0 0 0
210 0 ▶ if(!$self->getError()){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->getError) 0 0 0
211 0 my $result = $tt->process($self->getTemplate() . ".html", $self->_getTemplateVars(), \$content);
212 0 ▶ unless($result){
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
213 0 $self->setError($tt->error());
214 }
215 }
216 }
217 else{
218 0 $self->setError($Template::ERROR);
219 }
220 0 ▶ if($self->getError()){ #_parseFile may have errored
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getError) 0 0 0
221 0 my $result = $tt->process("genericerror.html", $self->_getTemplateVars(), \$content);
222 0 ▶ unless($result){
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
223 0 $self->getHector()->getLog()->log($tt->error(), 'error');
224 }
225 }
226 0 return $content;
227 }
228 ###########################################################
229 0 ▶sub _getTemplateNameForAction{
Subroutine _getTemplateNameForAction: not called CC=1 SCAR=6.9
230 0 my $self = shift;
231 0 my $hector = $self->getHector();
232 0 my $action = $hector->getAction();
233 0 $action =~ s/ /_/g; #remove spaces in action if any
234 0 return $action;
235 }
236 #########################################################
237 0 ▶sub _getTemplatePath{
Subroutine _getTemplatePath: not called CC=1 SCAR=6.9
238 0 $templateLoc;
239 }
240 ##############################################################
241  
242 =pod
243  
244 =head1 Notes
245  
246 If an error occurs a template called "genericerror.html" will be used instead of the specified template.
247 Please make sure you have this file, there is an example of this in the "root/templates" directory of this module.
248  
249 To change the template location use the following code at the top of your script:
250  
251 $PSGI::Hector::Response::SimpleTemplate::templateLoc = "../root";
252  
253 =head1 See also
254  
255 L<Template>
256  
257 =head1 Author
258  
259 MacGyveR <dumb@cpan.org>
260  
261 Development questions, bug reports, and patches are welcome to the above address.
262  
263 =head1 See Also
264  
265 =head1 Copyright
266  
267 Copyright (c) 2017 MacGyveR. All rights reserved.
268  
269 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
270  
271 =cut
272  
273 #########################################################
274 2 return 1;
« Hector/Request.pm ↑ Summary Hector/Middleware.pm »