| 1 | package Catalyst::View::MicroMason; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use base qw/Catalyst::View::Templated Class::Accessor/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | use Text::MicroMason; | ||
| 6 | use Class::C3; | ||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | # template_root is the old way of saying INCLUDE_PATH. don't use it. | ||
| 11 | __PACKAGE__->mk_accessors(qw(_template Mixins template_root)); | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Catalyst::View::MicroMason - MicroMason View Class | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | Use the helper: | ||
| 20 | |||
| 21 | script/create.pl view MicroMason MicroMason | ||
| 22 | |||
| 23 | To create a simple View subclass: | ||
| 24 | |||
| 25 | # lib/MyApp/View/MicroMason.pm | ||
| 26 | package MyApp::View::MicroMason; | ||
| 27 | use base 'Catalyst::View::MicroMason'; | ||
| 28 | 1; | ||
| 29 | |||
| 30 | And configure it in your app's config: | ||
| 31 | |||
| 32 | MyApp->config->{View::MicroMason} = { | ||
| 33 | # -Filters : to use |h and |u | ||
| 34 | # -ExecuteCache : to cache template output | ||
| 35 | # -CompileCache : to cache the templates | ||
| 36 | Mixins => [qw( -Filters -CompileCache )], | ||
| 37 | INCLUDE_PATH => '/path/to/comp_root' | ||
| 38 | }; | ||
| 39 | |||
| 40 | In an 'end' action: | ||
| 41 | |||
| 42 | $c->view('MicroMason')->template('foo.mc'); | ||
| 43 | $c->forward('MyApp::View::MicroMason'); | ||
| 44 | |||
| 45 | Or perhaps: | ||
| 46 | |||
| 47 | my $output = $c->view('MicroMason')->render('foo.mc'); | ||
| 48 | |||
| 49 | =head1 DESCRIPTION | ||
| 50 | |||
| 51 | Want to use a MicroMason component in your views? No problem! | ||
| 52 | Catalyst::View::MicroMason comes to the rescue. | ||
| 53 | |||
| 54 | =head1 METHODS | ||
| 55 | |||
| 56 | =head2 new | ||
| 57 | |||
| 58 | Create an instance; should be called from C<COMPONENT>, not by you. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub new { | ||
| 63 | my ($self, $c, $args) = @_; | ||
| 64 | shift; | ||
| 65 | |||
| 66 | my $real_include = $args->{INCLUDE_PATH}; | ||
| 67 | |||
| 68 | $self = $self->next::method(@_); | ||
| 69 | |||
| 70 | my $root = $real_include || | ||
| 71 | $self->template_root || | ||
| 72 | $c->config->{root}; | ||
| 73 | |||
| 74 | if (ref $root eq 'ARRAY' && @$root > 1) { | ||
| 75 | die "Catalyst::View::MicroMason only supports one entry in ". | ||
| 76 | "the INCLUDE_PATH or template_root."; | ||
| 77 | } | ||
| 78 | |||
| 79 | $root = $root->[0] if ref $root; | ||
| 80 | $self->template_root($root); | ||
| 81 | |||
| 82 | my @Mixins = @{ $self->Mixins || [] }; | ||
| 83 | push @Mixins, qw(-TemplateDir -AllowGlobals); | ||
| 84 | $self->_template(Text::MicroMason->new(@Mixins, | ||
| 85 | template_root => $root, | ||
| 86 | %$self | ||
| 87 | ) | ||
| 88 | ); | ||
| 89 | |||
| 90 | return $self; | ||
| 91 | } | ||
| 92 | |||
| 93 | =head2 process | ||
| 94 | |||
| 95 | Renders the component specified in $c->stash->{template} or by the | ||
| 96 | value $c->action (if $c->stash->{template} is undefined). See | ||
| 97 | L<Catalyst::View::Templated> for all the details. | ||
| 98 | |||
| 99 | MicroMason global variables C<$base>, C<$c> (or whatever you pass in | ||
| 100 | at config time as CATALYST_VAR) and c<$name> are automatically set to the base, | ||
| 101 | context and name of the app, respectively. | ||
| 102 | |||
| 103 | An exception is thrown if processing fails, otherwise the output is stored | ||
| 104 | in C<< $c->response->body >>. | ||
| 105 | |||
| 106 | =head2 render([$template]) | ||
| 107 | |||
| 108 | Renders the given template and returns output. | ||
| 109 | |||
| 110 | Throws an exception on error. If C<$template> is not defined, it is | ||
| 111 | determined by calling C<< $self->template >>. See | ||
| 112 | L<Catalyst::View::Templated> for details. | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | sub _render { | ||
| 117 | my ($self, $template, $stash, $args) = @_; | ||
| 118 | |||
| 119 | my $c_name = '$'. $self->{CATALYST_VAR}; | ||
| 120 | |||
| 121 | # Set the URL base, context and name of the app as global Mason vars | ||
| 122 | # $base, $c and $name | ||
| 123 | $self->_template->set_globals( '$base' => $self->context->req->base, | ||
| 124 | $c_name => $self->context, | ||
| 125 | '$name' => $self->context->config->{name}, | ||
| 126 | ); | ||
| 127 | |||
| 128 | delete $stash->{$self->{CATALYST_VAR}}; | ||
| 129 | delete $stash->{base}; | ||
| 130 | delete $stash->{name}; | ||
| 131 | |||
| 132 | warn "MicroMason: using a stash key called 'file' sets the template" | ||
| 133 | if exists $stash->{file}; | ||
| 134 | |||
| 135 | return $self->_template->execute(file => $template, %$stash); | ||
| 136 | } | ||
| 137 | |||
| 138 | |||
| 139 | =head1 SEE ALSO | ||
| 140 | |||
| 141 | L<Catalyst>, L<Catalyst::View::Templated>, L<Text::MicroMason>, | ||
| 142 | L<Catalyst::View::Mason> | ||
| 143 | |||
| 144 | =head1 AUTHOR | ||
| 145 | |||
| 146 | Jonas Alves C<< <jgda@cpan.org> >> | ||
| 147 | |||
| 148 | =head1 MAINTAINER | ||
| 149 | |||
| 150 | The Catalyst Core Team L<http://www.catalystframework.org> | ||
| 151 | |||
| 152 | Jonathan Rockway C<< <jrockway@cpan.org> >> | ||
| 153 | |||
| 154 | =head1 COPYRIGHT | ||
| 155 | |||
| 156 | This program is free software, you can redistribute it and/or modify it under | ||
| 157 | the same terms as Perl itself. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | 1; | ||