[Catalyst-commits] r6680 - in branches: .
templated_conversion/Catalyst-View-MicroMason/lib/Catalyst/View
jrockway at dev.catalyst.perl.org
jrockway at dev.catalyst.perl.org
Fri Aug 17 15:51:36 GMT 2007
Author: jrockway
Date: 2007-08-17 15:51:36 +0100 (Fri, 17 Aug 2007)
New Revision: 6680
Modified:
branches/
branches/templated_conversion/Catalyst-View-MicroMason/lib/Catalyst/View/MicroMason.pm
Log:
r28460 at foo: jon | 2007-08-17 09:08:35 -0500
convert MicroMason to Templated!
Property changes on: branches
___________________________________________________________________
Name: svk:merge
- d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst-branches:28459
+ d7608cd0-831c-0410-93c0-e5b306c3c028:/local/Catalyst-branches:28460
Modified: branches/templated_conversion/Catalyst-View-MicroMason/lib/Catalyst/View/MicroMason.pm
===================================================================
--- branches/templated_conversion/Catalyst-View-MicroMason/lib/Catalyst/View/MicroMason.pm 2007-08-17 14:51:28 UTC (rev 6679)
+++ branches/templated_conversion/Catalyst-View-MicroMason/lib/Catalyst/View/MicroMason.pm 2007-08-17 14:51:36 UTC (rev 6680)
@@ -1,13 +1,13 @@
package Catalyst::View::MicroMason;
use strict;
-use base qw/Catalyst::View Class::Accessor/;
+use base qw/Catalyst::View::Templated Class::Accessor/;
use Text::MicroMason;
-use NEXT;
+use Class::C3;
our $VERSION = '0.04';
-__PACKAGE__->mk_accessors(qw(template Mixins template_root));
+__PACKAGE__->mk_accessors(qw(_template Mixins template_root));
=head1 NAME
@@ -52,19 +52,32 @@
=cut
sub new {
- my ($self, $c) = @_;
+ my ($self, $c, $args) = @_;
shift;
+
+ my $real_include = $args->{INCLUDE_PATH};
- $self = $self->NEXT::new(@_);
- my $root = $self->template_root || $c->config->{root};
+ $self = $self->next::method(@_);
+
+ my $root = $real_include ||
+ $self->template_root ||
+ $c->config->{root};
+ if (ref $root && @$root > 1) {
+ die "Catalyst::View::MicroMason only supports one entry in ".
+ "the INCLUDE_PATH or template_root.";
+ }
+
+ $root = $root->[0] if ref $root;
+ $self->template_root($root);
+
my @Mixins = @{ $self->Mixins || [] };
push @Mixins, qw(-TemplateDir -AllowGlobals);
- $self->template(Text::MicroMason->new(@Mixins,
+ $self->_template(Text::MicroMason->new(@Mixins,
template_root => $root,
%$self
)
- );
+ );
return $self;
}
@@ -72,61 +85,44 @@
=head2 process
Renders the component specified in $c->stash->{template} or by the
-value $c->action (if $c->stash->{template} is undefined).
+value $c->action (if $c->stash->{template} is undefined). See
+L<Catalyst::View::Templated> for all the details.
-MicroMason global variables C<$base>, C<$c> and c<$name> are
-automatically set to the base, context and name of the app,
-respectively.
+MicroMason global variables C<$base>, C<$c> (or whatever you pass in
+at config time as CATALYST_VAR) and c<$name> are automatically set to the base,
+context and name of the app, respectively.
An exception is thrown if processing fails, otherwise the output is stored
in C<< $c->response->body >>.
-=cut
+=head2 render([$template])
-sub process {
- my ( $self, $c ) = @_;
-
- $c->response->headers->content_type('text/html;charset=utf8')
- unless $c->response->content_type;
-
- $c->res->body( $self->render($c) );
- return 1;
-}
+Renders the given template and returns output.
-=head2 render($c, [$template])
+Throws an exception on error. If C<$template> is not defined, it is
+determined by calling C<< $self->template >>. See
+L<Catalyst::View::Templated> for details.
-Renders the given template and returns output. Throws an exception on
-error. If $template is not defined, the value of "template" from the
-stash is used instead.
-
=cut
-sub render {
- my ($self, $c, $component_path) = @_;
- $component_path ||= $c->stash->{template} || $c->action;
+sub _render {
+ my ($self, $template, $stash, $args) = @_;
+
+ my $c_name = '$'. ($self->{CATALYST_VAR} || 'c');
# Set the URL base, context and name of the app as global Mason vars
# $base, $c and $name
- $self->template->set_globals(
- '$base' => $c->req->base,
- '$c' => $c,
- '$name' => $c->config->{name}
- );
+ $self->_template->set_globals(
+ '$base' => $self->context->req->base,
+ $c_name => $self->context,
+ '$name' => $self->context->config->{name},
+ );
- # render it
- my $output = eval {
- $self->template->execute(file => $component_path, %{$c->stash});
- };
+ delete $stash->{$self->{CATALYST_VAR} || 'c'};
+ delete $stash->{base};
+ delete $stash->{name};
- # errors?
- if (my $error = $@ ) {
- chomp $error;
- $error =
- qq/Couldn't render component "$component_path" - error was "$error"/;
- $c->error($error);
- }
-
- return $output;
+ return $self->_template->execute(file => $template, %$stash);
}
More information about the Catalyst-commits
mailing list