Version 0.32.
[catagits/Catalyst-View-TT.git] / lib / Catalyst / View / TT.pm
index bb9ff91..7277ddf 100644 (file)
@@ -1,13 +1,15 @@
 package Catalyst::View::TT;
 
 use strict;
+use warnings;
+
 use base qw/Catalyst::View/;
 use Data::Dump 'dump';
 use Template;
 use Template::Timer;
-use NEXT;
+use MRO::Compat;
 
-our $VERSION = '0.26';
+our $VERSION = '0.32';
 
 __PACKAGE__->mk_accessors('template');
 __PACKAGE__->mk_accessors('include_path');
@@ -21,6 +23,7 @@ Catalyst::View::TT - Template View Class
 =head1 SYNOPSIS
 
 # use the helper to create your View
+
     myapp_create.pl view TT TT
 
 # configure in lib/MyApp.pm (Could be set from configfile instead)
@@ -31,8 +34,8 @@ Catalyst::View::TT - Template View Class
         'View::TT' => {
             # any TT configurations items go here
             INCLUDE_PATH => [
-              MyApp->path_to( 'root', 'src' ), 
-              MyApp->path_to( 'root', 'lib' ), 
+              MyApp->path_to( 'root', 'src' ),
+              MyApp->path_to( 'root', 'lib' ),
             ],
             TEMPLATE_EXTENSION => '.tt',
             CATALYST_VAR => 'c',
@@ -42,25 +45,25 @@ Catalyst::View::TT - Template View Class
             WRAPPER            => 'site/wrapper',
         },
     );
-         
-# render view from lib/MyApp.pm or lib/MyApp::C::SomeController.pm
-    
+
+# render view from lib/MyApp.pm or lib/MyApp::Controller::SomeController.pm
+
     sub message : Global {
         my ( $self, $c ) = @_;
         $c->stash->{template} = 'message.tt2';
         $c->stash->{message}  = 'Hello World!';
-        $c->forward('MyApp::V::TT');
+        $c->forward( $c->view('TT') );
     }
 
 # access variables from template
 
     The message is: [% message %].
-    
+
     # example when CATALYST_VAR is set to 'Catalyst'
-    Context is [% Catalyst %]          
-    The base is [% Catalyst.req.base %] 
-    The name is [% Catalyst.config.name %] 
-    
+    Context is [% Catalyst %]
+    The base is [% Catalyst.req.base %]
+    The name is [% Catalyst.config.name %]
+
     # example when CATALYST_VAR isn't set
     Context is [% c %]
     The base is [% base %]
@@ -119,19 +122,19 @@ sub new {
         $c->log->debug( "TT Config: ", dump($config) );
     }
 
-    my $self = $class->NEXT::new(
-        $c, { %$config }, 
+    my $self = $class->next::method(
+        $c, { %$config },
     );
 
     # Set base include paths. Local'd in render if needed
     $self->include_path($config->{INCLUDE_PATH});
-    
+
     $self->config($config);
 
     # Creation of template outside of call to new so that we can pass [ $self ]
     # as INCLUDE_PATH config item, which then gets ->paths() called to get list
     # of include paths to search for templates.
-   
+
     # Use a weakend copy of self so we dont have loops preventing GC from working
     my $copy = $self;
     Scalar::Util::weaken($copy);
@@ -180,8 +183,8 @@ sub new {
             $config->{LOAD_TEMPLATES} = \@providers;
         }
     }
-    
-    $self->{template} = 
+
+    $self->{template} =
         Template->new($config) || do {
             my $error = Template->error();
             $c->log->error($error);
@@ -225,20 +228,20 @@ 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 = { 
+    my $vars = {
         (ref $args eq 'HASH' ? %$args : %{ $c->stash() }),
         $self->template_vars($c)
     };
 
-    local $self->{include_path} = 
+    local $self->{include_path} =
         [ @{ $vars->{additional_template_paths} }, @{ $self->{include_path} } ]
         if ref $vars->{additional_template_paths};
 
     unless ($self->template->process( $template, $vars, \$output ) ) {
-        return $self->template->error;  
+        return $self->template->error;
     } else {
         return $output;
     }
@@ -247,6 +250,7 @@ sub render {
 sub template_vars {
     my ( $self, $c ) = @_;
 
+    return  () unless $c;
     my $cvar = $self->config->{CATALYST_VAR};
 
     defined $cvar
@@ -274,16 +278,18 @@ of the Catalyst setup.
 
     $ script/myapp_create.pl view TT TT
 
-This creates a MyApp::V::TT.pm module in the F<lib> directory (again,
+This creates a MyApp::View::TT.pm module in the F<lib> directory (again,
 replacing C<MyApp> with the name of your application) which looks
 something like this:
 
-    package FooBar::V::TT;
-    
+    package FooBar::View::TT;
+
     use strict;
-     use base 'Catalyst::View::TT';
+    use warnings;
 
-    __PACKAGE__->config->{DEBUG} = 'all';
+    use base 'Catalyst::View::TT';
+
+    __PACKAGE__->config(DEBUG => 'all');
 
 Now you can modify your action handlers in the main application and/or
 controllers to forward to your view class.  You might choose to do this
@@ -291,24 +297,56 @@ in the end() method, for example, to automatically forward all actions
 to the TT view class.
 
     # In MyApp or MyApp::Controller::SomeController
-    
+
     sub end : Private {
         my( $self, $c ) = @_;
-        $c->forward('MyApp::V::TT');
+        $c->forward( $c->view('TT') );
     }
 
+But if you are using the standard auto-generated end action, you don't even need
+to do this!
+
+    # in MyApp::Controller::Root
+    sub end : ActionClass('RenderView') {} # no need to change this line
+
+    # in MyApp.pm
+    __PACKAGE__->config(
+        ...
+        default_view => 'TT',
+    );
+
+This will Just Work.  And it has the advantages that:
+
+=over 4
+
+=item *
+
+If you want to use a different view for a given request, just set 
+<< $c->stash->{current_view} >>.  (See L<Catalyst>'s C<< $c->view >> method
+for details.
+
+=item *
+
+<< $c->res->redirect >> is handled by default.  If you just forward to 
+C<View::TT> in your C<end> routine, you could break this by sending additional
+content.
+
+=back
+
+See L<Catalyst::Action::RenderView> for more details.
+
 =head2 CONFIGURATION
 
 There are a three different ways to configure your view class.  The
 first way is to call the C<config()> method in the view subclass.  This
 happens when the module is first loaded.
 
-    package MyApp::V::TT;
-    
+    package MyApp::View::TT;
+
     use strict;
     use base 'Catalyst::View::TT';
 
-    MyApp::V::TT->config({
+    MyApp::View::TT->config({
         INCLUDE_PATH => [
             MyApp->path_to( 'root', 'templates', 'lib' ),
             MyApp->path_to( 'root', 'templates', 'src' ),
@@ -320,7 +358,7 @@ happens when the module is first loaded.
 The second way is to define a C<new()> method in your view subclass.
 This performs the configuration when the view object is created,
 shortly after being loaded.  Remember to delegate to the base class
-C<new()> method (via C<$self-E<gt>NEXT::new()> in the example below) after
+C<new()> method (via C<$self-E<gt>next::method()> in the example below) after
 performing any configuration.
 
     sub new {
@@ -333,26 +371,26 @@ performing any configuration.
             PRE_PROCESS  => 'config/main',
             WRAPPER      => 'site/wrapper',
         });
-        return $self->NEXT::new(@_);
+        return $self->next::method(@_);
     }
+
 The final, and perhaps most direct way, is to define a class
 item in your main application configuration, again by calling the
-uniquitous C<config()> method.  The items in the class hash are
+ubiquitous C<config()> method.  The items in the class hash are
 added to those already defined by the above two methods.  This happens
 in the base class new() method (which is one reason why you must
-remember to call it via C<NEXT> if you redefine the C<new()> method in a
-subclass).
+remember to call it via C<MRO::Compat> if you redefine the C<new()>
+method in a subclass).
 
     package MyApp;
-    
+
     use strict;
     use Catalyst;
-    
+
     MyApp->config({
         name     => 'MyApp',
         root     => MyApp->path_to('root'),
-        'V::TT' => {
+        'View::TT' => {
             INCLUDE_PATH => [
                 MyApp->path_to( 'root', 'templates', 'lib' ),
                 MyApp->path_to( 'root', 'templates', 'src' ),
@@ -364,12 +402,12 @@ subclass).
 
 Note that any configuration items defined by one of the earlier
 methods will be overwritten by items of the same name provided by the
-latter methods.  
+latter methods.
 
 =head2 DYNAMIC INCLUDE_PATH
 
 Sometimes it is desirable to modify INCLUDE_PATH for your templates at run time.
+
 Additional paths can be added to the start of INCLUDE_PATH via the stash as
 follows:
 
@@ -391,19 +429,19 @@ checking and the chance of a memory leak:
 
     @{ $c->view('TT')->include_path } = qw/path another_path/;
 
-If you are calling C<render> directly then you can specify dynamic paths by 
+If you are calling C<render> directly then you can specify dynamic paths by
 having a C<additional_template_paths> key with a value of additonal directories
 to search. See L<CAPTURING TEMPLATE OUTPUT> for an example showing this.
 
 =head2 RENDERING VIEWS
 
 The view plugin renders the template specified in the C<template>
-item in the stash.  
+item in the stash.
 
     sub message : Global {
         my ( $self, $c ) = @_;
         $c->stash->{template} = 'message.tt2';
-        $c->forward('MyApp::V::TT');
+        $c->forward( $c->view('TT') );
     }
 
 If a stash item isn't defined, then it instead uses the
@@ -418,7 +456,7 @@ use as template variables.
         my ( $self, $c ) = @_;
         $c->stash->{template} = 'message.tt2';
         $c->stash->{message}  = 'Hello World!';
-        $c->forward('MyApp::V::TT');
+        $c->forward( $c->view('TT') );
     }
 
 A number of other template variables are also added:
@@ -446,7 +484,7 @@ L<Catalyst::Plugin::Email> and the L<render> method:
 
   sub send_email : Local {
     my ($self, $c) = @_;
-    
+
     $c->email(
       header => [
         To      => 'me@localhost',
@@ -469,32 +507,47 @@ See L<C<TIMER>> property of the L<config> method.
 
 =head2 new
 
-The constructor for the TT view. Sets up the template provider, 
+The constructor for the TT view. Sets up the template provider,
 and reads the application config.
 
-=head2 process
+=head2 process($c)
 
 Renders the template specified in C<< $c->stash->{template} >> or
-C<< $c->action >> (the private name of the matched action.  Calls L<render> to
+C<< $c->action >> (the private name of the matched action).  Calls L<render> to
 perform actual rendering. Output is stored in C<< $c->response->body >>.
 
+It is possible to forward to the process method of a TT view from inside
+Catalyst like this:
+
+    $c->forward('View::TT');
+
+N.B. This is usually done automatically by L<Catalyst::Action::RenderView>.
+
 =head2 render($c, $template, \%args)
 
 Renders the given template and returns output, or a L<Template::Exception>
-object upon error. 
+object upon error.
 
-The template variables are set to C<%$args> if $args is a hashref, or 
-$C<< $c->stash >> otherwise. In either case the variables are augmented with 
+The template variables are set to C<%$args> if $args is a hashref, or
+$C<< $c->stash >> otherwise. In either case the variables are augmented with
 C<base> set to C< << $c->req->base >>, C<c> to C<$c> and C<name> to
 C<< $c->config->{name} >>. Alternately, the C<CATALYST_VAR> configuration item
 can be defined to specify the name of a template variable through which the
 context reference (C<$c>) can be accessed. In this case, the C<c>, C<base> and
 C<name> variables are omitted.
 
-C<$template> can be anything that Template::process understands how to 
+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.
+
+It is possible to forward to the render method of a TT view from inside Catalyst
+to render page fragments like this:
+
+    my $fragment = $c->forward("View::TT", "render", $template_name, $c->stash->{fragment_data});
+
 =head2 template_vars
 
 Returns a list of keys/values to be used as the catalyst variables in the
@@ -509,7 +562,7 @@ the TT configuration hash, or to set the options as below:
 
 The list of paths TT will look for templates in.
 
-=head2 C<CATALYST_VAR> 
+=head2 C<CATALYST_VAR>
 
 Allows you to change the name of the Catalyst context object. If set, it will also
 remove the base and name aliases, so you will have access them through <context>.
@@ -519,7 +572,7 @@ For example:
     MyApp->config({
         name     => 'MyApp',
         root     => MyApp->path_to('root'),
-        'V::TT' => {
+        'View::TT' => {
             CATALYST_VAR => 'Catalyst',
         },
     });
@@ -553,8 +606,8 @@ a sufix to add when looking for templates bases on the C<match> method in L<Cata
 
 For example:
 
-  package MyApp::C::Test;
-  sub test : Local { .. } 
+  package MyApp::Controller::Test;
+  sub test : Local { .. }
 
 Would by default look for a template in <root>/test/test. If you set TEMPLATE_EXTENSION to '.tt', it will look for
 <root>/test/test.tt.
@@ -566,7 +619,7 @@ Allows you to specify the template providers that TT will use.
     MyApp->config({
         name     => 'MyApp',
         root     => MyApp->path_to('root'),
-        'V::TT' => {
+        'View::TT' => {
             PROVIDERS => [
                 {
                     name    => 'DBI',
@@ -592,7 +645,7 @@ plus:
 
 You can also specify the 'copy_config' key as an arrayref, to copy those keys
 from the general config, into the config for the provider:
-    
+
     DEFAULT_ENCODING    => 'utf-8',
     PROVIDERS => [
         {
@@ -600,7 +653,7 @@ from the general config, into the config for the provider:
             copy_config => [qw(DEFAULT_ENCODING INCLUDE_PATH)]
         }
     ]
-    
+
 This can prove useful when you want to use the additional_template_paths hack
 in your own provider, or if you need to use Template::Provider::Encoding
 
@@ -620,6 +673,17 @@ further.  It also creates a default set of templates to get you
 started.  It also configures the view module to locate the templates
 automatically.
 
+=head1 NOTES
+
+If you are using the L<CGI> module inside your templates, you will
+experience that the Catalyst server appears to hang while rendering
+the web page. This is due to the debug mode of L<CGI> (which is
+waiting for input in the terminal window). Turning off the
+debug mode using the "-no_debug" option solves the
+problem, eg.:
+
+    [% USE CGI('-no_debug') %]
+
 =head1 SEE ALSO
 
 L<Catalyst>, L<Catalyst::Helper::View::TT>,
@@ -637,7 +701,7 @@ Andy Wardley, C<abw@cpan.org>
 
 =head1 COPYRIGHT
 
-This program is free software, you can redistribute it and/or modify it 
+This program is free software. You can redistribute it and/or modify it
 under the same terms as Perl itself.
 
 =cut