blib/lib/Catalyst/View/Mason.pm

filter: Statementstmt 100% 79 / 79 Branchbran 91.6% 22 / 24 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 96.9% 125 / 129 CC 15 SCAR 12.6
CC
15
Cov
97%
render
6.0
get_component_path
4.0
process
4.0
CRAP 3.5
↑ Summary blib/lib/Catalyst/Helper/View/Mason.pm »
1 package Catalyst::View::Mason;
2  
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 ▶use base qw/Catalyst::View/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use HTML::Mason;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 16 our $VERSION = '0.19';
12  
13 16 __PACKAGE__->mk_accessors('template');
14  
15 =head1 NAME
16  
17 Catalyst::View::Mason - Mason View Class
18  
19 =head1 SYNOPSIS
20  
21 # use the helper
22 script/create.pl view Mason Mason
23  
24 # lib/MyApp/View/Mason.pm
25 package MyApp::View::Mason;
26  
27 use base 'Catalyst::View::Mason';
28  
29 __PACKAGE__->config(use_match => 0);
30  
31 1;
32  
33 $c->forward('MyApp::View::Mason');
34  
35 =head1 DESCRIPTION
36  
37 Want to use a Mason component in your views? No problem!
38 Catalyst::View::Mason comes to the rescue.
39  
40 =head1 EXAMPLE
41  
42 From the Catalyst controller:
43  
44 $c->stash->{name} = 'Homer'; # Pass a scalar
45 $c->stash->{extra_info} = {
46 last_name => 'Simpson',
47 children => [qw(Bart Lisa Maggie)]
48 }; # A ref works too
49  
50 From the Mason template:
51  
52 <%args>
53 $name
54 $extra_info
55 </%args>
56 <p>Your name is <strong><% $name %> <% $extra_info->{last_name} %></strong>
57 <p>Your children are:
58 <ul>
59 % foreach my $child (@{$extra_info->{children}}) {
60 <li><% $child %></li>
61 % }
62 </ul>
63  
64 =head1 METHODS
65  
66 =cut
67  
68 =head2 new($app, \%config)
69  
70 =cut
71  
72 105 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
73 105 my ($self, $app, $arguments) = @_;
74  
75 my %config = (
76 comp_root => $app->config->{root},
77 data_dir => File::Spec->catdir(
78 File::Spec->tmpdir,
79 sprintf('%s_%d_mason_data_dir', $app, $<),
80 ),
81 use_match => 0,
82 allow_globals => [],
83 template_extension => q//,
84 always_append_template_extension => 0,
85 105 %{ $self->config },
86 105 %{ $arguments },
87 );
88  
89 # stringify data_dir
90 105 $config{data_dir} .= q//;
91  
92 # stringify comp_root if it isn't an unblessed array reference already
93 $config{comp_root} .= q//
94 105 ▶ if blessed($config{comp_root}) || ref $config{comp_root} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($config{"comp_root"}) or ref $config{"comp_root"} ne "ARRAY" 104 1 105
Condition100%3/3
builtin::blessed($config{"comp_root"}) or ref $config{"comp_root"} ne "ARRAY"
l!l&&r!l&&!r
93111
MC/DC100%2/2
builtin::blessed($config{"comp_root"}) or ref $config{"comp_root"} ne "ARRAY"
builtin::blessed($config{"comp_root"}) ref $config{"comp_root"} ne "ARRAY"
Truth table100%3/3
A or B
A= builtin::blessed($config{"comp_root"})B= ref $config{"comp_root"} ne "ARRAY"
ABresult
1X1
011
000
95  
96 105 unshift @{ $config{allow_globals} }, qw/$c $base $name/;
97 105 $self = $self->next::method($app, \%config);
98 105 $self->{output} = q//;
99  
100 105 $self->config({ %config });
101  
102 # those are config options for the view, not mason itself.
103 105 delete @config{qw/
104 use_match
105 template_extension
106 always_append_template_extension
107 catalyst_component_name
108 /};
109  
110 105 ▶ if ($self->config->{use_match}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"use_match"}) 1 104 105
111 1 $app->log->warn(sprintf(<<'EOW', ref $self));
112 DEPRECATION WARNING: %s sets the use_match config variable to a true value.
113 This has been deprecated. Please see the Catalyst::View::Mason
114 documentation for details on use_match.
115 EOW
116 }
117  
118 $self->template(
119 HTML::Mason::Interp->new(
120 %config,
121 out_method => \$self->{output},
122 )
123 105 );
124  
125 101 return $self;
126 }
127  
128 =head2 get_component_path
129  
130 Returns the component path from $c->stash->{template} or
131 $c->request->match or $c->action (depending on the use_match setting).
132  
133 =cut
134  
135 16 ▶sub get_component_path {
Subroutine get_component_path: called CC=4 SCAR=13.9
Pod documented
136 16 my ($self, $c) = @_;
137  
138 16 my $component_path = $c->stash->{template};
139 16 my $extension = $self->config->{template_extension};
140  
141 16 ▶ if (defined $component_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $component_path) { } 3 13 16
142 $component_path .= $extension
143 3 ▶ if $self->config->{always_append_template_extension};
Branch: 1 branch
BranchTrueFalseTotal
if $self->config->{"always_append_template_extension"} 1 2 3
144 }
145 else {
146 $component_path = $self->config->{use_match}
147 13 ▶ ? $c->request->match
Branch: 1 branch
BranchTrueFalseTotal
$self->config->{'use_match'} ? : 0 13 13
148 : $c->action;
149  
150 13 $component_path .= $extension;
151 }
152  
153 16 return $component_path;
154 }
155  
156 =head2 process
157  
158 Renders the component specified in $c->stash->{template} or $c->request->match
159 or $c->action (depending on the use_match setting) to $c->response->body.
160  
161 Note that the component name must be absolute, or is converted to absolute
162 (i.e., a / is added to the beginning if it doesn't start with one).
163  
164 Mason global variables C<$base>, C<$c>, and C<$name> are automatically
165 set to the base, context, and name of the app, respectively.
166  
167 =cut
168  
169 17 ▶sub process {
Subroutine process: called CC=4 SCAR=13.9
Pod documented
170 17 my ($self, $c) = @_;
171  
172 17 my $component_path = $self->get_component_path($c);
173 17 my $output = $self->render($c, $component_path);
174  
175 17 ▶ if (blessed($output) && $output->isa('HTML::Mason::Exception')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($output) and $output->isa("HTML::Mason::Exception")) 1 16 17
Condition66%2/3
builtin::blessed($output) and $output->isa("HTML::Mason::Exception")
!ll&&!rl&&r
1601
MC/DC50%1/2
builtin::blessed($output) and $output->isa("HTML::Mason::Exception")
builtin::blessed($output) $output->isa("HTML::Mason::Exception")
Truth table66%2/3
A and B
A= builtin::blessed($output)B= $output->isa("HTML::Mason::Exception")
ABresult
0X0
100
111
176 1 chomp $output;
177 1 my $error = qq/Couldn't render component "$component_path" - error was "$output"/;
178 1 $c->log->error($error);
179 1 $c->error($error);
180 1 return 0;
181 }
182  
183 16 ▶ unless ($c->response->content_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->response->content_type) 15 1 16
184 15 $c->response->content_type('text/html; charset=utf-8');
185 }
186  
187 16 $c->response->body($output);
188  
189 16 return 1;
190 }
191  
192 =head2 render($c, $component_path, \%args)
193  
194 Renders the given template and returns output, or a HTML::Mason::Exception
195 object upon error.
196  
197 The template variables are set to %$args if $args is a hashref, or
198 $c-E<gt>stash otherwise.
199  
200 =cut
201  
202 19 ▶sub _default_globals {
Subroutine _default_globals: called CC=1 SCAR=0.0
203 19 my ($self, $c) = @_;
204  
205 my %default_globals = (
206 '$c' => $c,
207 '$base' => $c->request->base,
208 '$name' => $c->config->{name},
209 19 );
210  
211 19 return %default_globals;
212 }
213  
214 19 ▶sub render {
Subroutine render: called CC=6 SCAR=17.9
Pod documented
215 19 my ($self, $c, $component_path, $args) = @_;
216  
217 19 ▶ if ($component_path !~ m{^/}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($component_path =~ m[^/]) 17 2 19
218 17 $component_path = '/' . $component_path;
219 }
220  
221 19 ▶ $c->log->debug(qq/Rendering component "$component_path"/) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 19 19
222  
223 # Set the URL base, context and name of the app as global Mason vars
224 # $base, $c and $name
225 19 my %default_globals = $self->_default_globals($c);
226 19 ▶ while (my ($key, $val) = each %default_globals) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %default_globals) 57 19 76
227 57 $self->template->set_global($key => $val);
228 }
229  
230 19 $self->{output} = q//;
231  
232 19 eval {
233 $self->template->exec(
234 $component_path,
235 19 ▶ ref $args eq 'HASH' ? %{ $args } : %{ $c->stash },
Branch: 1 branch
BranchTrueFalseTotal
ref $args eq 'HASH' ? : 2 17 19
236 );
237 };
238  
239 19 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 2 17 19
240 2 return $error;
241 }
242  
243 17 return $self->{output};
244 }
245  
246 =head3 config
247  
248 This allows you to to pass additional settings to the HTML::Mason::Interp
249 constructor or to set the options as below:
250  
251 =over
252  
253 =item C<template_extension>
254  
255 This string is appended (if present) to C<< $c->action >> when generating a
256 template path.
257  
258 Defaults to an empty string.
259  
260 Example: C<< template_extension => '.html' >>
261  
262 =item C<always_append_template_extension>
263  
264 Set this to a true value if you want C<template_extension> to be appended to
265 the component path even if it was explicitly set.
266  
267 Defaults to 0.
268  
269 Example: C<< always_append_template_extension => 1 >>
270  
271 =item C<use_match>
272  
273 Use C<$c-E<gt>request-E<gt>match> instead of C<$c-E<gt>action> to determine
274 which template to use if C<$c-E<gt>stash-E<gt>{template}> isn't set. This option
275 is deprecated and exists for backward compatibility only.
276  
277 Currently defaults to 0. Old code should set this to 1 to avoid breakage.
278  
279 Example: C<< use_match => 0 >>
280  
281 =back
282  
283 The default HTML::Mason::Interp config options are as follows:
284  
285 =over
286  
287 =item C<comp_root>
288  
289 C<$app-E<gt>config-E<gt>root>
290  
291 =item C<data_dir>
292  
293 C<File::Spec-E<gt>catdir( File::Spec-E<gt>tmpdir, sprintf('%s_%d_mason_data_dir', $app, $E<lt>) )>
294  
295 =item C<allow_globals>
296  
297 C<qw/$c $name $base/>
298  
299 If you add additional allowed globals those will be appended to the list of
300 default globals.
301  
302 =back
303  
304 =cut
305  
306 =head1 SEE ALSO
307  
308 L<Catalyst>, L<HTML::Mason>, "Using Mason from a Standalone Script" in L<HTML::Mason::Admin>
309  
310 =head1 AUTHORS
311  
312 =over 4
313  
314 =item Andres Kievsky C<ank@cpan.org>
315  
316 =item Sebastian Riedel C<sri@cpan.org>
317  
318 =item Marcus Ramberg
319  
320 =item Florian Ragwitz C<rafl@debian.org>
321  
322 =item Justin Hunter C<justin.d.hunter@gmail.com>
323  
324 =back
325  
326 =head1 COPYRIGHT
327  
328 Copyright (c) 2005 - 2009
329 the Catalyst::View::Mason L</AUTHORS>
330 as listed above.
331  
332 This program is free software, you can redistribute it and/or modify it under
333 the same terms as Perl itself.
334  
335 =cut
336  
337 16 1;
↑ Summary blib/lib/Catalyst/Helper/View/Mason.pm »