[Catalyst-commits] r11677 - in Catalyst-View-Petal/branches/render/lib/Catalyst/View: . Petal

hobbs at dev.catalyst.perl.org hobbs at dev.catalyst.perl.org
Wed Oct 28 00:34:03 GMT 2009


Author: hobbs
Date: 2009-10-28 00:34:02 +0000 (Wed, 28 Oct 2009)
New Revision: 11677

Added:
   Catalyst-View-Petal/branches/render/lib/Catalyst/View/Petal/
   Catalyst-View-Petal/branches/render/lib/Catalyst/View/Petal/Exception.pm
Modified:
   Catalyst-View-Petal/branches/render/lib/Catalyst/View/Petal.pm
Log:
Initial commit -- works. Could really use tests.


Added: Catalyst-View-Petal/branches/render/lib/Catalyst/View/Petal/Exception.pm
===================================================================
--- Catalyst-View-Petal/branches/render/lib/Catalyst/View/Petal/Exception.pm	                        (rev 0)
+++ Catalyst-View-Petal/branches/render/lib/Catalyst/View/Petal/Exception.pm	2009-10-28 00:34:02 UTC (rev 11677)
@@ -0,0 +1,22 @@
+package Catalyst::View::Petal::Exception;
+
+use strict;
+use warnings;
+
+our $VERSION = '0.10';
+
+sub new {
+  my $class = shift;
+
+  return bless { @_ }, $class;
+}
+
+sub message {
+  return shift->{message};
+}
+
+sub template {
+  return shift->{template};
+}
+
+1;

Modified: Catalyst-View-Petal/branches/render/lib/Catalyst/View/Petal.pm
===================================================================
--- Catalyst-View-Petal/branches/render/lib/Catalyst/View/Petal.pm	2009-10-27 23:58:18 UTC (rev 11676)
+++ Catalyst-View-Petal/branches/render/lib/Catalyst/View/Petal.pm	2009-10-28 00:34:02 UTC (rev 11677)
@@ -4,8 +4,9 @@
 use base 'Catalyst::View';
 
 use Petal;
+use Catalyst::View::Petal::Exception;
 
-our $VERSION = '0.03';
+our $VERSION = '0.10';
 
 =head1 NAME
 
@@ -44,8 +45,8 @@
 
 =item process
 
-Renders the template specified in C<< $c->stash->{template} >> or C<<
-$c->request->match >>.
+Renders the template specified in C<< $c->stash->{template} >> or 
+C<< $c->request->match >>.
 Template variables are set up from the contents of C<< $c->stash >>,
 augmented with C<base> set to C<< $c->req->base >>, C<c> to C<$c> and
 C<name> to C<< $c->config->{name} >>.  Output is stored in
@@ -63,46 +64,70 @@
         return 0;
     }
 
-    my %options = (
-        base_dir => [ $c->config->{root}, $c->config->{root} . "/base" ],
-        file     => $file
-    );
+    my $output = $self->render($c, $file);
 
-    unless ( $c->debug ) {
-        $options{debug_dump}         = 0;
-        $options{error_on_undef_var} = 0;
+    if (UNIVERSAL::isa($output, 'Catalyst::View::Petal::Exception')) {
+      my $error = qq/Couldn't render template "/ . $output->template 
+          . qq/". Error: "/ . $output->message . qq/"/;
+      $c->log->error($error);
+      $c->error($error);
+      return 0;
     }
 
-    my $process = {
-        base => $c->req->base,
-        c    => $c,
-        name => $c->config->{name},
-        %{ $c->stash }
-    };
+    unless ( $c->response->headers->content_type ) {
+        $c->res->headers->content_type('text/html; charset=utf-8');
+    }
 
-    $c->log->debug(qq/Rendering template "$file"/) if $c->debug;
+    $c->response->body($output);
 
-    my $petal = Petal->new( %options, %{ $self->config } );
+    return 1;
+}
 
-    my $body;
+=item render
 
-    eval { $body = $petal->process($process) };
+Renders the template specified by C<$template> and returns the output.
+If C<$args> is provided and is a hashref, then use the values in C<$args> as
+template variables; otherwise use C<< $c->stash >>. The C<c>, C<base>, and
+C<name> variables will be set as with C<process>.
 
-    if ( my $error = $@ ) {
-        chomp $error;
-        $error = qq/Couldn't render template "$file". Error: "$error"/;
-        $c->log->error($error);
-        $c->error($error);
-        return 0;
-    }
+On error, returns a L<Catalyst::View::Petal::Exception> object.
 
-    unless ( $c->response->headers->content_type ) {
-        $c->res->headers->content_type('text/html; charset=utf-8');
-    }
+=cut
 
-    $c->response->body($body);
+sub render {
+  my ( $self, $c, $template, $args ) = @_;
 
-    return 1;
+  my %options = (
+    base_dir => [ $c->config->{root}, $c->config->{root} . "/base" ],
+    file     => $template
+  );
+
+  unless ( $c->debug ) {
+    $options{debug_dump}         = 0;
+    $options{error_on_undef_var} = 0;
+  }
+
+  my $process = {
+    (ref $args eq 'HASH' ? %$args : %{ $c->stash() }),
+    base => $c->req->base,
+    c    => $c,
+    name => $c->config->{name},
+  };
+
+  $c->log->debug(qq/Rendering template "$template"/) if $c->debug;
+
+  my $petal = Petal->new( %options, %{ $self->config } );
+
+  my $body = eval { $petal->process($process) };
+
+  # Try to make this work kind of like View::TT does -- on error return
+  # a trapped exception. Except Petal doesn't have an exception class...
+  if ( my $error = $@ ) {
+    chomp $error;
+    return Catalyst::View::Petal::Exception->new(message => $error, template => $template);
+  } else {
+    return $body;
+  }
 }
 
 =item config




More information about the Catalyst-commits mailing list