Add support for running render without a context
Marcus Ramberg [Sat, 12 Sep 2009 21:47:18 +0000 (21:47 +0000)]
Changes
lib/Catalyst/View/TT.pm
t/11norequest.t

diff --git a/Changes b/Changes
index f6fafb0..052a629 100644 (file)
--- a/Changes
+++ b/Changes
@@ -11,6 +11,7 @@ Revision history for Perl extension Catalyst::View::TT.
         - Expand TTSite documentation (RT #33838)
         - Added a test for direct rendering of a template from a view object,
           without a request.
+        - Added support for running render with a undef context.
 
 0.29    2009-02-20 14:43:00
         - Remove extra unwanted .gitignore from manifest
index f60af73..1dcc88a 100644 (file)
@@ -228,7 +228,7 @@ sub process {
 sub render {
     my ($self, $c, $template, $args) = @_;
 
-    $c->log->debug(qq/Rendering template "$template"/) if $c->debug;
+    $c->log->debug(qq/Rendering template "$template"/) if $c && $c->debug;
 
     my $output;
     my $vars = {
@@ -250,6 +250,7 @@ sub render {
 sub template_vars {
     my ( $self, $c ) = @_;
 
+    return  () unless $c;
     my $cvar = $self->config->{CATALYST_VAR};
 
     defined $cvar
@@ -500,6 +501,9 @@ C<$template> can be anything that Template::process understands how to
 process, including the name of a template file or a reference to a test string.
 See L<Template::process|Template/process> for a full list of supported formats.
 
+To use the render method outside of your Catalyst app, just pass a undef context. 
+This can be useful for tests, for instance.
+
 =head2 template_vars
 
 Returns a list of keys/values to be used as the catalyst variables in the
index 9a562b0..0aff98a 100644 (file)
@@ -1,13 +1,12 @@
 use strict;
 use warnings;
-use Test::More tests => 4;
+use Test::More tests => 3;
 
 use FindBin;
 use lib "$FindBin::Bin/lib";
 
 BEGIN { use_ok 'TestApp' or die }
 
-ok my $c  = TestApp->new, 'Instantiate app object';
-ok my $tt = $c->view('TT'), 'Get TT view object';
-is $tt->render($c, 'test.tt', { message => 'hello' }), 'hello',
+ok my $tt = TestApp->view('TT'), 'Get TT view object';
+is $tt->render(undef, 'test.tt', { message => 'hello' }), 'hello',
     'render() should return the template output';