line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#response object |
2
|
|
|
|
|
|
|
package CGI::Mungo::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 = $mungo->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 Mungo actions to just display a static page. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 METHODS |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=cut |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
1
|
|
1383
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
32
|
|
26
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
27
|
1
|
|
|
1
|
|
5
|
use File::Basename; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
78
|
|
28
|
1
|
|
|
1
|
|
5
|
use File::Spec; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
21
|
|
29
|
1
|
|
|
1
|
|
824
|
use Template; |
|
1
|
|
|
|
|
124468
|
|
|
1
|
|
|
|
|
46
|
|
30
|
1
|
|
|
1
|
|
14
|
use Carp; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
581
|
|
31
|
1
|
|
|
1
|
|
9
|
use base qw(CGI::Mungo::Response::Base); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2545
|
|
32
|
|
|
|
|
|
|
our $templateLoc = "../root/templates"; #where the templates are stored |
33
|
|
|
|
|
|
|
######################################################### |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head2 new($mungo) |
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
|
|
|
|
|
|
|
sub new{ |
44
|
0
|
|
|
0
|
1
|
|
my($class, $mungo) = @_; |
45
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new($mungo); |
46
|
0
|
|
|
|
|
|
$self->{'_template'} = undef; |
47
|
0
|
|
|
|
|
|
$self->{'_templateVars'} = {}; |
48
|
0
|
|
|
|
|
|
bless $self, $class; |
49
|
0
|
|
|
|
|
|
$self->setTemplateVar("env", \%ENV); #include this var by default |
50
|
0
|
|
|
|
|
|
$self->setTemplateVar("mungo", $mungo); #this will be handy to have too |
51
|
0
|
|
|
|
|
|
$self->setTemplateVar("action", $mungo->getAction()); #this will be handy to have too |
52
|
0
|
|
|
|
|
|
$self->setTemplateVar("debug", $mungo->getOption("debug")); |
53
|
0
|
|
|
|
|
|
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 |
66
|
|
|
|
|
|
|
section for more details. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=cut |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
######################################################### |
71
|
|
|
|
|
|
|
sub setTemplate{ |
72
|
0
|
|
|
0
|
1
|
|
my($self, $template) = @_; |
73
|
0
|
|
|
|
|
|
$self->{'_template'} = $template; |
74
|
0
|
|
|
|
|
|
return 1; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
######################################################### |
77
|
|
|
|
|
|
|
sub getTemplate{ |
78
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
79
|
0
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub display{ #this sub will display the page headers if needed |
100
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
101
|
0
|
|
|
|
|
|
my $output; |
102
|
0
|
0
|
|
|
|
|
if(!$self->getTemplate()){ #if no template has been set in the action sub then we set a default |
103
|
0
|
|
|
|
|
|
my $tName = $self->_getTemplateNameForAction(); |
104
|
0
|
|
|
|
|
|
$self->setTemplate($tName); #set the template automatically |
105
|
|
|
|
|
|
|
} |
106
|
0
|
0
|
0
|
|
|
|
if(!$self->getError() && !$self->header("Location") && !$self->getTemplate()){ #we must have a template set if we dont have an error or a redirect |
|
|
|
0
|
|
|
|
|
107
|
0
|
|
|
|
|
|
$self->setError("No template defined"); |
108
|
|
|
|
|
|
|
} |
109
|
0
|
0
|
|
|
|
|
if($self->_getDisplayedHeader()){ #just display more content |
110
|
0
|
|
|
|
|
|
$output = $self->_getContent(); #get the contents of the template |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
else{ #first output so display any headers |
113
|
0
|
0
|
|
|
|
|
if($self->header("Location")){ |
114
|
0
|
|
|
|
|
|
$self->code(302); |
115
|
0
|
|
|
|
|
|
$self->message('Found'); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
else{ #if we dont have a redirect |
118
|
0
|
0
|
|
|
|
|
if(!$self->header("Content-type")){ #set default content type |
119
|
0
|
|
|
|
|
|
$self->header("Content-type" => "text/html"); |
120
|
|
|
|
|
|
|
} |
121
|
0
|
|
|
|
|
|
my $content = $self->_getContent(); #get the contents of the template |
122
|
0
|
|
|
|
|
|
$self->content($content); |
123
|
|
|
|
|
|
|
} |
124
|
0
|
0
|
0
|
|
|
|
if($self->getError() && $self->code() =~ m/^[123]/){ #set the error code when needed |
125
|
0
|
|
|
|
|
|
$self->code(500); |
126
|
0
|
|
|
|
|
|
$self->message('Internal Server Error'); |
127
|
|
|
|
|
|
|
} |
128
|
0
|
|
|
|
|
|
$output = "Status: " . $self->as_string(); |
129
|
|
|
|
|
|
|
} |
130
|
0
|
0
|
|
|
|
|
if($self->getError()){ |
131
|
0
|
|
|
|
|
|
$self->log($self->getError()); #just log it so we have a record of this |
132
|
|
|
|
|
|
|
} |
133
|
0
|
|
|
|
|
|
print $output; |
134
|
0
|
|
|
|
|
|
$self->_setDisplayedHeader(); #we wont display the header again |
135
|
0
|
|
|
|
|
|
return 1; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
######################################################### |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=pod |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 setError($message) |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$response->setError("something has broken"); |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Set an error message for the response, which is accessible in the error template |
146
|
|
|
|
|
|
|
as [% message %]. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
######################################################### |
151
|
|
|
|
|
|
|
sub setError(){ |
152
|
0
|
|
|
0
|
1
|
|
my($self, $message) = @_; |
153
|
0
|
|
|
|
|
|
$self->setTemplateVar("message", $message); #so we can access the error message via smarty |
154
|
0
|
|
|
|
|
|
return $self->SUPER::setError($message); #save the message for later in the instance |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
######################################################### |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=pod |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head2 setTemplateVar($name, $value) |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
$response->setTemplatevar("name", "Bob"); |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Creates a template variable with the specified name and value. |
165
|
|
|
|
|
|
|
The variable may be of any type and can be access from the template in the |
166
|
|
|
|
|
|
|
usual way. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=cut |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
######################################################### |
171
|
|
|
|
|
|
|
sub setTemplateVar{ |
172
|
0
|
|
|
0
|
1
|
|
my($self, $name, $value) = @_; |
173
|
0
|
|
|
|
|
|
$self->{'_templateVars'}->{$name} = $value; |
174
|
0
|
|
|
|
|
|
return 1; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
######################################################### |
177
|
|
|
|
|
|
|
sub getTemplateVar{ |
178
|
0
|
|
|
0
|
0
|
|
my($self, $name) = @_; |
179
|
0
|
|
|
|
|
|
my $vars = $self->_getTemplateVars(); |
180
|
0
|
|
|
|
|
|
return $vars->{$name}; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
######################################################### |
183
|
|
|
|
|
|
|
# private methods |
184
|
|
|
|
|
|
|
######################################################## |
185
|
|
|
|
|
|
|
sub _getTemplateVars{ |
186
|
0
|
|
|
0
|
|
|
my $self = shift; |
187
|
0
|
|
|
|
|
|
return $self->{'_templateVars'}; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
######################################################### |
190
|
|
|
|
|
|
|
sub _getContent{ |
191
|
0
|
|
|
0
|
|
|
my $self = shift; |
192
|
0
|
|
|
|
|
|
my $content = ""; |
193
|
0
|
|
|
|
|
|
my $tt = Template->new( |
194
|
|
|
|
|
|
|
{ |
195
|
|
|
|
|
|
|
INCLUDE_PATH => $self->_getTemplatePath(), |
196
|
|
|
|
|
|
|
ENCODING => 'utf-8' |
197
|
|
|
|
|
|
|
} |
198
|
|
|
|
|
|
|
); |
199
|
0
|
0
|
|
|
|
|
if($tt){ |
200
|
0
|
0
|
|
|
|
|
if(!$self->getError()){ |
201
|
0
|
0
|
|
|
|
|
if(!$tt->process($self->getTemplate() . ".html", $self->_getTemplateVars(), \$content)){ |
202
|
0
|
|
|
|
|
|
$self->setError($tt->error()); |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
else{ |
207
|
0
|
|
|
|
|
|
$self->setError($Template::ERROR); |
208
|
|
|
|
|
|
|
} |
209
|
0
|
0
|
|
|
|
|
if($self->getError()){ #_parseFile may have errored |
210
|
0
|
|
|
|
|
|
$self->setTemplateVar('message', $self->getError()); |
211
|
0
|
|
|
|
|
|
$tt->process("genericerror.html", $self->_getTemplateVars(), \$content); |
212
|
|
|
|
|
|
|
} |
213
|
0
|
|
|
|
|
|
return $content; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
########################################################### |
216
|
|
|
|
|
|
|
sub _getTemplateNameForAction{ |
217
|
0
|
|
|
0
|
|
|
my $self = shift; |
218
|
0
|
|
|
|
|
|
my $mungo = $self->getMungo(); |
219
|
0
|
|
|
|
|
|
my $action = $mungo->getAction(); |
220
|
0
|
|
|
|
|
|
my $script = $mungo->_getScriptName(); |
221
|
0
|
|
|
|
|
|
$script =~ s/\.[^\.]+$//; #remove the file extension |
222
|
0
|
|
|
|
|
|
$action =~ s/ /_/g; #remove spaces in action if any |
223
|
0
|
|
|
|
|
|
my $name = $script . "-" . $action; |
224
|
0
|
|
|
|
|
|
return $name; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
######################################################### |
227
|
|
|
|
|
|
|
sub _getTemplatePath{ |
228
|
0
|
|
|
0
|
|
|
my $currentDir = dirname($ENV{'SCRIPT_NAME'}); |
229
|
0
|
|
|
|
|
|
my @dirs = File::Spec->splitdir($currentDir); |
230
|
0
|
|
|
|
|
|
shift(@dirs); |
231
|
0
|
|
|
|
|
|
@dirs = map("..", @dirs); |
232
|
0
|
|
|
|
|
|
return File::Spec->catfile(@dirs, $templateLoc); |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
############################################################## |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=pod |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head1 Notes |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
If an error occurs a template called "genericerror.html" will be used instead of the specified template. |
241
|
|
|
|
|
|
|
Please make sure you have this file, there is an example of this in the "root/templates" directory of this module. |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
To change the template location use the following code at the top of your script: |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
$CGI::Mungo::Response::SimpleTemplate::templateLoc = "../root"; |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=head1 Sess also |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
L |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head1 Author |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
MacGyveR |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
Development questions, bug reports, and patches are welcome to the above address |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head1 Copyright |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
Copyright (c) 2012 MacGyveR. All rights reserved. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=cut |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
######################################################### |
266
|
|
|
|
|
|
|
return 1; |