Catalyst::View::TT man page on Pidora

Man page or keyword search:  
man Server   31170 pages
apropos Keyword Search (all sections)
Output format
Pidora logo
[printable version]

Catalyst::View::TT(3) User Contributed Perl DocumentationCatalyst::View::TT(3)

NAME
       Catalyst::View::TT - Template View Class

SYNOPSIS
       # use the helper to create your View

	   myapp_create.pl view Web TT

       # add custom configration in View/Web.pm

	   __PACKAGE__->config(
	       # any TT configuration items go here
	       TEMPLATE_EXTENSION => '.tt',
	       CATALYST_VAR => 'c',
	       TIMER	    => 0,
	       ENCODING	    => 'utf-8'
	       # Not set by default
	       PRE_PROCESS	  => 'config/main',
	       WRAPPER		  => 'site/wrapper',
	       render_die => 1, # Default for new apps, see render method docs
	       expose_methods => [qw/method_in_view_class/],
	   );

       # add include path configuration in MyApp.pm

	   __PACKAGE__->config(
	       'View::Web' => {
		   INCLUDE_PATH => [
		       __PACKAGE__->path_to( 'root', 'src' ),
		       __PACKAGE__->path_to( 'root', 'lib' ),
		   ],
	       },
	   );

       # 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( $c->view('Web') );
	   }

       # 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 %]

	   # example when CATALYST_VAR isn't set
	   Context is [% c %]
	   The base is [% base %]
	   The name is [% name %]

DESCRIPTION
       This is the Catalyst view class for the Template Toolkit.  Your
       application should defined a view class which is a subclass of this
       module. Throughout this manual it will be assumed that your application
       is named MyApp and you are creating a TT view named Web; these names
       are placeholders and should always be replaced with whatever name
       you've chosen for your application and your view. The easiest way to
       create a TT view class is through the myapp_create.pl script that is
       created along with the application:

	   $ script/myapp_create.pl view Web TT

       This creates a MyApp::View::Web.pm module in the lib directory (again,
       replacing "MyApp" with the name of your application) which looks
       something like this:

	   package FooBar::View::Web;
	   use Moose;

	   extends '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
       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( $c->view('Web') );
	   }

       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 => 'Web',
	   );

       This will Just Work.  And it has the advantages that:

       ·   If you want to use a different view for a given request, just set
	   << $c->stash->{current_view} >>.  (See Catalyst's "$c->view" method
	   for details.

       ·   << $c->res->redirect >> is handled by default.  If you just forward
	   to "View::Web" in your "end" routine, you could break this by
	   sending additional content.

       See Catalyst::Action::RenderView for more details.

   CONFIGURATION
       There are a three different ways to configure your view class.  The
       first way is to call the "config()" method in the view subclass.	 This
       happens when the module is first loaded.

	   package MyApp::View::Web;
	   use Moose;
	   extends 'Catalyst::View::TT';

	   __PACKAGE__->config({
	       PRE_PROCESS  => 'config/main',
	       WRAPPER	    => 'site/wrapper',
	   });

       You may also override the configuration provided in the view class by
       adding a 'View::Web' section to your application config.

       This should generally be used to inject the include paths into the view
       to avoid the view trying to load the application to resolve paths.

	   .. inside MyApp.pm ..
	   __PACKAGE__->config(
	       'View::Web' => {
		   INCLUDE_PATH => [
		       __PACKAGE__->path_to( 'root', 'templates', 'lib' ),
		       __PACKAGE__->path_to( 'root', 'templates', 'src' ),
		   ],
	       },
	   );

       You can also configure your view from within your config file if you're
       using Catalyst::Plugin::ConfigLoader. This should be reserved for
       deployment-specific concerns. For example:

	   # MyApp_local.conf (Config::General format)

	   <View Web>
	     WRAPPER "custom_wrapper"
	     INCLUDE_PATH __path_to('root/templates/custom_site')__
	     INCLUDE_PATH __path_to('root/templates')__
	   </View>

       might be used as part of a simple way to deploy different instances of
       the same application with different themes.

   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:

	   $c->stash->{additional_template_paths} =
	       [$c->config->{root} . '/test_include_path'];

       If you need to add paths to the end of INCLUDE_PATH, there is also an
       include_path() accessor available:

	   push( @{ $c->view('Web')->include_path }, qw/path/ );

       Note that if you use include_path() to add extra paths to INCLUDE_PATH,
       you MUST check for duplicate paths. Without such checking, the above
       code will add "path" to INCLUDE_PATH at every request, causing a memory
       leak.

       A safer approach is to use include_path() to overwrite the array of
       paths rather than adding to it. This eliminates both the need to
       perform duplicate checking and the chance of a memory leak:

	   @{ $c->view('Web')->include_path } = qw/path another_path/;

       If you are calling "render" directly then you can specify dynamic paths
       by having a "additional_template_paths" key with a value of additonal
       directories to search. See "CAPTURING TEMPLATE OUTPUT" for an example
       showing this.

   Unicode
       Be sure to set "ENCODING => 'utf-8'" and use
       Catalyst::Plugin::Unicode::Encoding if you want to use non-ascii
       characters (encoded as utf-8) in your templates.

   RENDERING VIEWS
       The view plugin renders the template specified in the "template" item
       in the stash.

	   sub message : Global {
	       my ( $self, $c ) = @_;
	       $c->stash->{template} = 'message.tt2';
	       $c->forward( $c->view('Web') );
	   }

       If a stash item isn't defined, then it instead uses the stringification
       of the action dispatched to (as defined by $c->action) in the above
       example, this would be "message", but because the default is to append
       '.tt', it would load "root/message.tt".

       The items defined in the stash are passed to the Template Toolkit for
       use as template variables.

	   sub default : Private {
	       my ( $self, $c ) = @_;
	       $c->stash->{template} = 'message.tt2';
	       $c->stash->{message}  = 'Hello World!';
	       $c->forward( $c->view('Web') );
	   }

       A number of other template variables are also added:

	   c	  A reference to the context object, $c
	   base	  The URL base, from $c->req->base()
	   name	  The application name, from $c->config->{ name }

       These can be accessed from the template in the usual way:

       <message.tt2>:

	   The message is: [% message %]
	   The base is [% base %]
	   The name is [% name %]

       The output generated by the template is stored in "$c->response->body".

   CAPTURING TEMPLATE OUTPUT
       If you wish to use the output of a template for some other purpose than
       displaying in the response, e.g. for sending an email, this is possible
       using Catalyst::Plugin::Email and the render method:

	 sub send_email : Local {
	   my ($self, $c) = @_;

	   $c->email(
	     header => [
	       To      => 'me@localhost',
	       Subject => 'A TT Email',
	     ],
	     body => $c->view('Web')->render($c, 'email.tt', {
	       additional_template_paths => [ $c->config->{root} . '/email_templates'],
	       email_tmpl_param1 => 'foo'
	       }
	     ),
	   );
	 # Redirect or display a message
	 }

   TEMPLATE PROFILING
       See "TIMER" property of the config method.

   METHODS
   new
       The constructor for the TT view. Sets up the template provider, and
       reads the application config.

   process($c)
       Renders the template specified in "$c->stash->{template}" or
       "$c->action" (the private name of the matched action).  Calls render to
       perform actual rendering. Output is stored in "$c->response->body".

       It is possible to forward to the process method of a TT view from
       inside Catalyst like this:

	   $c->forward('View::Web');

       N.B. This is usually done automatically by
       Catalyst::Action::RenderView.

   render($c, $template, \%args)
       Renders the given template and returns output. Throws a
       Template::Exception object upon error.

       The template variables are set to %$args if $args is a hashref, or
       "$c->stash" otherwise. In either case the variables are augmented with
       "base" set to "$c->req->base", "c" to $c, and "name" to
       "$c->config->{name}". Alternately, the "CATALYST_VAR" configuration
       item can be defined to specify the name of a template variable through
       which the context reference ($c) can be accessed. In this case, the
       "c", "base", and "name" variables are omitted.

       $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 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::Web", "render", $template_name, $c->stash->{fragment_data});

       Backwards compatibility note

       The render method used to just return the Template::Exception object,
       rather than just throwing it. This is now deprecated and instead the
       render method will throw an exception for new applications.

       This behaviour can be activated (and is activated in the default
       skeleton configuration) by using "render_die => 1". If you rely on the
       legacy behaviour then a warning will be issued.

       To silence this warning, set "render_die => 0", but it is recommended
       you adjust your code so that it works with "render_die => 1".

       In a future release, "render_die => 1" will become the default if
       unspecified.

   template_vars
       Returns a list of keys/values to be used as the catalyst variables in
       the template.

   config
       This method allows your view subclass to pass additional settings to
       the TT configuration hash, or to set the options as below:

   paths
       The list of paths TT will look for templates in.

   expose_methods
       The list of methods in your View class which should be made available
       to the templates.

       For example:

	 expose_methods => [qw/uri_for_css/],

	 ...

	 sub uri_for_css {
	   my ($self, $c, $filename) = @_;

	   # additional complexity like checking file exists here

	   return $c->uri_for('/static/css/' . $filename);
	 }

       Then in the template:

	 [% uri_for_css('home.css') %]

   "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>.

       For example, if CATALYST_VAR has been set to "Catalyst", a template
       might contain:

	   The base is [% Catalyst.req.base %]
	   The name is [% Catalyst.config.name %]

   "TIMER"
       If you have configured Catalyst for debug output, and turned on the
       TIMER setting, "Catalyst::View::TT" will enable profiling of template
       processing (using Template::Timer). This will embed HTML comments in
       the output from your templates, such as:

	   <!-- TIMER START: process mainmenu/mainmenu.ttml -->
	   <!-- TIMER START: include mainmenu/cssindex.tt -->
	   <!-- TIMER START: process mainmenu/cssindex.tt -->
	   <!-- TIMER END: process mainmenu/cssindex.tt (0.017279 seconds) -->
	   <!-- TIMER END: include mainmenu/cssindex.tt (0.017401 seconds) -->

	   ....

	   <!-- TIMER END: process mainmenu/footer.tt (0.003016 seconds) -->

   "TEMPLATE_EXTENSION"
       a sufix to add when looking for templates bases on the "match" method
       in Catalyst::Request.

       For example:

	 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.

   "PROVIDERS"
       Allows you to specify the template providers that TT will use.

	   MyApp->config(
	       name	=> 'MyApp',
	       root	=> MyApp->path_to('root'),
	       'View::Web' => {
		   PROVIDERS => [
		       {
			   name	   => 'DBI',
			   args	   => {
			       DBI_DSN => 'dbi:DB2:books',
			       DBI_USER=> 'foo'
			   }
		       }, {
			   name	   => '_file_',
			   args	   => {}
		       }
		   ]
	       },
	   );

       The 'name' key should correspond to the class name of the provider you
       want to use.  The _file_ name is a special case that represents the
       default TT file-based provider.	By default the name is will be
       prefixed with 'Template::Provider::'.  You can fully qualify the name
       by using a unary plus:

	   name => '+MyApp::Provider::Foo'

       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 => [
	       {
		   name	   => 'Encoding',
		   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

   "CLASS"
       Allows you to specify a custom class to use as the template class
       instead of Template.

	   package MyApp::View::Web;
	   use Moose;
	   extends 'Catalyst::View::TT';

	   use Template::AutoFilter;

	   __PACKAGE__->config({
	       CLASS => 'Template::AutoFilter',
	   });

       This is useful if you want to use your own subclasses of Template, so
       you can, for example, prevent XSS by automatically filtering all output
       through "| html".

   HELPERS
       The Catalyst::Helper::View::TT and Catalyst::Helper::View::TTSite
       helper modules are provided to create your view module.	There are
       invoked by the myapp_create.pl script:

	   $ script/myapp_create.pl view Web TT

	   $ script/myapp_create.pl view Web TTSite

       The Catalyst::Helper::View::TT module creates a basic TT view module.
       The Catalyst::Helper::View::TTSite module goes a little further.	 It
       also creates a default set of templates to get you started.  It also
       configures the view module to locate the templates automatically.

NOTES
       If you are using the 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 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') %]

SEE ALSO
       Catalyst, Catalyst::Helper::View::TT, Catalyst::Helper::View::TTSite,
       Template::Manual

AUTHORS
       Sebastian Riedel, "sri@cpan.org"

       Marcus Ramberg, "mramberg@cpan.org"

       Jesse Sheidlower, "jester@panix.com"

       Andy Wardley, "abw@cpan.org"

       Luke Saunders, "luke.saunders@gmail.com"

COPYRIGHT
       This program is free software. You can redistribute it and/or modify it
       under the same terms as Perl itself.

perl v5.14.2			  2012-02-15		 Catalyst::View::TT(3)
[top]

List of man pages available for Pidora

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Tweet
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
....................................................................
Vote for polarhome
Free Shell Accounts :: the biggest list on the net