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