| 1 | package Catalyst::View::Mason2; | ||||||||||||||||||||
| 2 | 2 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 2 | $Catalyst::View::Mason2::VERSION = '0.03'; | |||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | 2 | ▶ | use Mason; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use base qw(Catalyst::View); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 2 | __PACKAGE__->mk_accessors(qw(interp)); | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 14 | 2 | my ( $class, $c, $arguments ) = @_; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | my %config = ( | ||||||||||||||||||||
| 17 | comp_root => $c->path_to( 'root', 'comps' ), | ||||||||||||||||||||
| 18 | mason_root_class => 'Mason', | ||||||||||||||||||||
| 19 | plugins => [], | ||||||||||||||||||||
| 20 | 2 | %{ $class->config }, | |||||||||||||||||||
| 21 | 2 | %{$arguments}, | |||||||||||||||||||
| 22 | ); | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # Stringify comp_root and data_dir if they are objects | ||||||||||||||||||||
| 25 | # | ||||||||||||||||||||
| 26 | 2 | foreach my $key (qw(comp_root data_dir)) { | |||||||||||||||||||
| 27 | 4 | ▶ | $config{$key} .= "" if blessed( $config{$key} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # Add globals | ||||||||||||||||||||
| 31 | # | ||||||||||||||||||||
| 32 | 2 | push( @{ $config{allow_globals} }, '$c' ); | |||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | # Call superclass to create initial object | ||||||||||||||||||||
| 35 | # | ||||||||||||||||||||
| 36 | 2 | my $self = $class->next::method( $c, \%config ); | |||||||||||||||||||
| 37 | 2 | $self->config( {%config} ); | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | # Remove non-Mason parameters. | ||||||||||||||||||||
| 40 | # | ||||||||||||||||||||
| 41 | 2 | my $mason_root_class = delete( $config{mason_root_class} ); | |||||||||||||||||||
| 42 | 2 | delete @config{qw(catalyst_component_name)}; | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | # Create and store the interp | ||||||||||||||||||||
| 45 | # | ||||||||||||||||||||
| 46 | 2 | my $interp = $mason_root_class->new(%config); | |||||||||||||||||||
| 47 | 2 | $self->interp($interp); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 2 | return $self; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 3 | ▶ | sub get_component_path { | ||||||||||||||||||
|
Subroutine
get_component_path: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 53 | 3 | my ( $self, $c ) = @_; | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | # If template was specified in stash, use that; otherwise use the action. | ||||||||||||||||||||
| 56 | # | ||||||||||||||||||||
| 57 | 3 | ▶ | my $path = $c->stash->{template} || $c->action; | ||||||||||||||||||
|
Condition66%2/3
$c->stash->{'template'} || $c->action
MC/DC0%0/2
$c->stash->{'template'} || $c->action
$c->stash->{'template'}
$c->action
Truth table66%2/3
A || B
A= $c->stash->{'template'}B= $c->action
| |||||||||||||||||||||
| 58 | 3 | ▶ | $path = "/$path" if substr( $path, 0, 1 ) ne '/'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 3 | return $path; | |||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 3 | ▶ | sub process { | ||||||||||||||||||
|
Subroutine
process: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 64 | 3 | my ( $self, $c ) = @_; | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 3 | my $path = $self->get_component_path($c); | |||||||||||||||||||
| 67 | 3 | my $output = $self->render( $c, $path, $c->stash ); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 3 | ▶ | unless ( $c->response->content_type ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 70 | 0 | $c->response->content_type('text/html; charset=utf-8'); | |||||||||||||||||||
| 71 | } | ||||||||||||||||||||
| 72 | 3 | $c->response->body($output); | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 3 | return 1; | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 3 | ▶ | sub render { | ||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 78 | 3 | my ( $self, $c, $path, $args ) = @_; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 3 | $self->interp->set_global( '$c' => $c ); | |||||||||||||||||||
| 81 | 3 | return $self->interp->run( $path, %$args )->output; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 2 | 1; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =pod | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =head1 NAME | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | Catalyst::View::Mason2 - Mason 2.x view class | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =head1 VERSION | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | version 0.03 | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =head1 SYNOPSIS | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | # use the helper | ||||||||||||||||||||
| 101 | script/create.pl view Mason2 Mason2 | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | # lib/MyApp/View/Mason2.pm | ||||||||||||||||||||
| 104 | package MyApp::View::Mason2; | ||||||||||||||||||||
| 105 | use base 'Catalyst::View::Mason2'; | ||||||||||||||||||||
| 106 | __PACKAGE__->config( | ||||||||||||||||||||
| 107 | # insert Mason parameters here | ||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 1; | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | # in a controller | ||||||||||||||||||||
| 113 | package MyApp::Controller::Foo; | ||||||||||||||||||||
| 114 | sub bar : Local { | ||||||||||||||||||||
| 115 | ... | ||||||||||||||||||||
| 116 | $c->stash->{name} = 'Homer'; | ||||||||||||||||||||
| 117 | $c->stash->{template} = 'foo/bar'; # .mc is automatically added | ||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # in root/comps/foo/bar.mc | ||||||||||||||||||||
| 121 | <%args> | ||||||||||||||||||||
| 122 | $.name | ||||||||||||||||||||
| 123 | </%args> | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | Hello <% $.name %>! Your URI was <% $c->req->uri %>. | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =head1 DESCRIPTION | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | Allows you to use L<Mason 2.x|Mason> for your views. | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | =for readme stop | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head1 VIEW CONFIGURATION | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | =over | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =item mason_root_class | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | Class to use for creating the Mason object. Defaults to 'Mason'. | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =back | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | =head1 MASON CONSTRUCTOR | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | Other than any special mentioned keys above, the configuration for this view | ||||||||||||||||||||
| 146 | will be passed directly into C<< Mason->new >>. | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | There are a few defaults specific to this view: | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | =over | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | =item comp_root | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | If not provided, defaults C<< $c->path_to('root', 'comps') >>. | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | =item data_dir | ||||||||||||||||||||
| 157 | |||||||||||||||||||||
| 158 | If not provided, defaults C<< $c->path_to('data') >>. | ||||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | =item allow_globals | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | Automatically includes C<$c>. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =back | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | All other defaults are standard Mason. | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | =head1 GLOBALS | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | All components have access to C<$c>, the current Catalyst context. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =head1 METHODS | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | =over | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =item process ($c) | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | Renders the component specified in C<< $c->stash->{template} >> or, if not | ||||||||||||||||||||
| 179 | specified, C<< $c->action >>. | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | The component path is prefixed with a '/' if it does not already have one, and | ||||||||||||||||||||
| 182 | Mason will automatically add a ".mc" extension - to change the latter, you can | ||||||||||||||||||||
| 183 | use | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | __PACKAGE__->config( | ||||||||||||||||||||
| 186 | autoextend_request_path => 0 | ||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | Request arguments are taken from C<< $c->stash >>. | ||||||||||||||||||||
| 190 | |||||||||||||||||||||
| 191 | =item render ($c, $path, \%args) | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | Renders the component C<$path> with C<\%args>, and returns the output. | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | =back | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | =for readme continue | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | =head1 AUTHOR | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | Jonathan Swartz <swartz@pobox.com> | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | This software is copyright (c) 2011 by Jonathan Swartz. | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 208 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | =cut | ||||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | __END__ | ||||||||||||||||||||