| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Mason2Renderer; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
25823
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
30
|
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
35
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
4010
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
|
1
|
|
|
|
|
10156
|
|
|
|
1
|
|
|
|
|
6
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
4265
|
use Mason; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Mason::Interp; |
|
10
|
|
|
|
|
|
|
use Mason::Request; |
|
11
|
|
|
|
|
|
|
use Mason::Result; |
|
12
|
|
|
|
|
|
|
use Encode; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 NAME |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
Mojolicious::Plugin::Mason2Renderer - Mason 2 (aka Mason 2.x) Renderer Plugin. |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 VERSION |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Version 0.03 |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=cut |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
## Mojolicious::Lite |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# example -1- |
|
31
|
|
|
|
|
|
|
use Mojolicious::Lite; |
|
32
|
|
|
|
|
|
|
plugin 'mason2_renderer'; |
|
33
|
|
|
|
|
|
|
get '/' => sub { |
|
34
|
|
|
|
|
|
|
my $self = shift; |
|
35
|
|
|
|
|
|
|
$self->render('/index', handler => "mason" ); |
|
36
|
|
|
|
|
|
|
}; |
|
37
|
|
|
|
|
|
|
app->start; |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# template: MOJO_HOME/mason/index.mc |
|
40
|
|
|
|
|
|
|
<html> |
|
41
|
|
|
|
|
|
|
<body>Welcome</body> |
|
42
|
|
|
|
|
|
|
</html> |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# example -2- |
|
45
|
|
|
|
|
|
|
use Mojolicious::Lite; |
|
46
|
|
|
|
|
|
|
plugin 'mason2_renderer' => { preload_regexps => [ '.mc$', '/path/to/comps/to/preload', ... ], |
|
47
|
|
|
|
|
|
|
interp_params => { comp_root => "/path/to/mason/comps", |
|
48
|
|
|
|
|
|
|
... (other parameters to the new() Mason::Interp constructor) |
|
49
|
|
|
|
|
|
|
}, |
|
50
|
|
|
|
|
|
|
request_params => { ... (other parameters to the new() Mason::Request constructor) |
|
51
|
|
|
|
|
|
|
}, |
|
52
|
|
|
|
|
|
|
}; |
|
53
|
|
|
|
|
|
|
get '/' => sub { |
|
54
|
|
|
|
|
|
|
my $self = shift; |
|
55
|
|
|
|
|
|
|
$self->render('/index', handler => "mason", mytext => "Hello world" ); |
|
56
|
|
|
|
|
|
|
}; |
|
57
|
|
|
|
|
|
|
app->start; |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# template: /path/to/mason/comps/index.mc |
|
60
|
|
|
|
|
|
|
<%args> |
|
61
|
|
|
|
|
|
|
$mytext => undef |
|
62
|
|
|
|
|
|
|
</%args> |
|
63
|
|
|
|
|
|
|
<html> |
|
64
|
|
|
|
|
|
|
<body>Welcome : <% $mytext %></body> |
|
65
|
|
|
|
|
|
|
</html> |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
## Mojolicious |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# example -1- |
|
71
|
|
|
|
|
|
|
package MyApp; |
|
72
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious'; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub startup { |
|
75
|
|
|
|
|
|
|
my $self = shift; |
|
76
|
|
|
|
|
|
|
$self->plugin('mason2_renderer'); |
|
77
|
|
|
|
|
|
|
$self->routes->get('/' => sub { |
|
78
|
|
|
|
|
|
|
my $self = shift; |
|
79
|
|
|
|
|
|
|
$self->render('/index', handler => "mason" ); |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
); |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
1; |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# template: MOJO_HOME/mason/index.mc |
|
86
|
|
|
|
|
|
|
<html> |
|
87
|
|
|
|
|
|
|
<body>Welcome</body> |
|
88
|
|
|
|
|
|
|
</html> |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# example -2- |
|
91
|
|
|
|
|
|
|
package MyApp; |
|
92
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious'; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub startup { |
|
95
|
|
|
|
|
|
|
my $self = shift; |
|
96
|
|
|
|
|
|
|
$self->plugin('mason2_renderer', { preload_regexps => [ '.mc$', '/path/to/comps/to/preload', ... ], |
|
97
|
|
|
|
|
|
|
interp_params => { comp_root => "/path/to/mason/comps", |
|
98
|
|
|
|
|
|
|
... (other parameters to the new() Mason::Interp constructor) |
|
99
|
|
|
|
|
|
|
}, |
|
100
|
|
|
|
|
|
|
request_params => { ... (other parameters to the new() Mason::Request constructor) |
|
101
|
|
|
|
|
|
|
}, |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
); |
|
104
|
|
|
|
|
|
|
$self->routes->get('/' => sub { |
|
105
|
|
|
|
|
|
|
my $self = shift; |
|
106
|
|
|
|
|
|
|
$self->render('/index', handler => "mason", mytext => "Hello World" ); |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
); |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
1; |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# template: /path/to/mason/comps/index.mc |
|
113
|
|
|
|
|
|
|
<%args> |
|
114
|
|
|
|
|
|
|
$mytext => undef |
|
115
|
|
|
|
|
|
|
</%args> |
|
116
|
|
|
|
|
|
|
<html> |
|
117
|
|
|
|
|
|
|
<body> |
|
118
|
|
|
|
|
|
|
Welcome : <% $mytext %><br/> |
|
119
|
|
|
|
|
|
|
Mason root_comp is <% $c->app->home %><br/> |
|
120
|
|
|
|
|
|
|
</body> |
|
121
|
|
|
|
|
|
|
</html> |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
L<Mojolicous::Plugin::Mason2Renderer> is a renderer for L<Mason> 2 (aka L<Mason> 2.x) template system. |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head2 Mojolicious::Controller object aka. $c |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Mason templates have access to the L<Mojolicious::Controller> object as global $c. |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 Mason comp_root |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
C<comp_root> is set to default "MOJO_HOME/mason" |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 METHODS |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
L<Mojolicious::Plugin::Mason2Renderer> inherits all methods from L<Mojolicious::Plugin> and implements the following new ones. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 register |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
$plugin->register; |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Register renderer in L<Mojolicious> application. |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=cut |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub register { |
|
149
|
|
|
|
|
|
|
my ($self, $app, $conf) = @_; |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
# Config |
|
152
|
|
|
|
|
|
|
$conf ||= {}; |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# Mason::Interp params |
|
155
|
|
|
|
|
|
|
$conf->{interp_params}->{comp_root} ||= $app->home->rel_dir('mason'); |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
my @allow_globals = ('$c', @{$conf->{interp_params}->{allow_globals} || []}); |
|
158
|
|
|
|
|
|
|
$conf->{interp_params}->{allow_globals} = \@allow_globals; |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# make Mason::Interp |
|
162
|
|
|
|
|
|
|
my $interp = Mason->new(%{$conf->{interp_params}}); |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# preload comps ? |
|
166
|
|
|
|
|
|
|
if( (defined $conf->{preload_regexps}) && (ref($conf->{preload_regexps}) eq "ARRAY") ) { |
|
167
|
|
|
|
|
|
|
foreach my $regexp (@{$conf->{preload_regexps}}) { |
|
168
|
|
|
|
|
|
|
foreach my $comp ($interp->all_paths) { |
|
169
|
|
|
|
|
|
|
next if($comp=~/.+~$/); |
|
170
|
|
|
|
|
|
|
$interp->load($comp) if($comp =~ /$regexp/); |
|
171
|
|
|
|
|
|
|
} |
|
172
|
|
|
|
|
|
|
} |
|
173
|
|
|
|
|
|
|
} |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
# Mason::Request params |
|
177
|
|
|
|
|
|
|
$conf->{request_params} ||= {}; |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
my $request_params = $conf->{request_params}; |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
# Add "mason" handler |
|
183
|
|
|
|
|
|
|
$app->renderer->add_handler( |
|
184
|
|
|
|
|
|
|
mason => sub { |
|
185
|
|
|
|
|
|
|
my ($r, $c, $output, $options) = @_; |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
# check $interp object |
|
188
|
|
|
|
|
|
|
if(not $interp) { |
|
189
|
|
|
|
|
|
|
$c->app->log->error("Mason::Interp not initialized"); |
|
190
|
|
|
|
|
|
|
$c->render_exception("Mason::Interp not initialized"); |
|
191
|
|
|
|
|
|
|
$$output = ""; |
|
192
|
|
|
|
|
|
|
return 0; |
|
193
|
|
|
|
|
|
|
} |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# stash contains args to pass to Mason |
|
196
|
|
|
|
|
|
|
my $stash = $c->stash; |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# template name |
|
199
|
|
|
|
|
|
|
return 0 unless my $template = $options->{template}; |
|
200
|
|
|
|
|
|
|
$template =~ s,^/*,/,; # Mason component must start with / |
|
201
|
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# set global "$c" in Mason environment |
|
203
|
|
|
|
|
|
|
$interp->set_global('c' => $c); |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
# call Mason interpreter |
|
207
|
|
|
|
|
|
|
$request_params->{out_method} = $output; |
|
208
|
|
|
|
|
|
|
my $result = $interp->run( $request_params, $template, %{$stash}); |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
if(not $result) { |
|
211
|
|
|
|
|
|
|
$c->app->log->error("Mason::Request not initialized"); |
|
212
|
|
|
|
|
|
|
$c->render_exception("Mason::Request not initialized"); |
|
213
|
|
|
|
|
|
|
$$output = ""; |
|
214
|
|
|
|
|
|
|
return 0; |
|
215
|
|
|
|
|
|
|
} |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# Encoding |
|
219
|
|
|
|
|
|
|
$$output = decode($r->encoding, $$output) if $r->encoding; |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# All seems OK |
|
223
|
|
|
|
|
|
|
return 1; |
|
224
|
|
|
|
|
|
|
} |
|
225
|
|
|
|
|
|
|
); |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
} |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
L<Mojolicious>, L<Mojolicious::Plugin>, L<http://mojolicio.us>. |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
Mason 1, L<HTML::Mason>, L<http://www.masonhq.com>. |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Mason 2, L<Mason>. |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
Mason 1 Mojolicious Plugin, L<Mojolicious::Plugin::Mason1Renderer> |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
=head1 AUTHOR |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
Alexandre SIMON, C<< <asimon at cpan.org> >> |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head1 BUGS and SUPPORT |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
perldoc Mojolicious::Plugin::Mason2Renderer |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Bugs and feature requests will be tracked at RT: |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Plugin-Mason2Renderer |
|
253
|
|
|
|
|
|
|
bug-mojolicious-plugin-mason2renderer at rt.cpan.org |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
The latest source code can be browsed and fetched at: |
|
256
|
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
https://github.com/igit/Mojolicious-Plugin-Mason2Renderer |
|
258
|
|
|
|
|
|
|
git clone git://github.com/igit/Mojolicious-Plugin-Mason2Renderer.git |
|
259
|
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
You can also look for information at: |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
=over 4 |
|
264
|
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Plugin-Mason2Renderer> |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
L<http://annocpan.org/dist/Mojolicious-Plugin-Mason2Renderer> |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=item * CPAN Ratings |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
L<http://cpanratings.perl.org/d/Mojolicious-Plugin-Mason2Renderer> |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=item * Search CPAN |
|
278
|
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
L<http://search.cpan.org/dist/Mojolicious-Plugin-Mason2Renderer/> |
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=back |
|
282
|
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Original idea was taken from Graham BARR (L<http://search.cpan.org/~gbarr/>) |
|
287
|
|
|
|
|
|
|
MojoX::Renderer::Mason module. This module was not longer adapted to Mojolicious new |
|
288
|
|
|
|
|
|
|
Plugin philosophy. |
|
289
|
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
Many, many thanks to Sebastian RIEDEL for developping Mojolicious and Jonathan SWARTZ |
|
291
|
|
|
|
|
|
|
for developping HTML::Mason and Mason (2). |
|
292
|
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
Copyright 2011 Alexandre SIMON. |
|
296
|
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
|
298
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
|
299
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
|
300
|
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
See http://dev.perl.org/licenses/ for more information. |
|
302
|
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=cut |
|
305
|
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
1; # End of Mojolicious::Plugin::Mason2Renderer |