CGI::Application::Plugin::TT

CGI::Application::Plugin::TT is a Perl module to add Template Toolkit support to CGI::Application.
Download

CGI::Application::Plugin::TT Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Cees Hek
  • Publisher web site:
  • http://search.cpan.org/~ceeshek/CGI-Application-Plugin-TT-1.04/lib/CGI/Application/Plugin/TT.pm

CGI::Application::Plugin::TT Tags


CGI::Application::Plugin::TT Description

CGI::Application::Plugin::TT is a Perl module to add Template Toolkit support to CGI::Application. CGI::Application::Plugin::TT is a Perl module to add Template Toolkit support to CGI::Application.SYNOPSIS use base qw(CGI::Application); use CGI::Application::Plugin::TT; sub myrunmode { my $self = shift; my %params = ( email => 'email@company.com', menu => , session_obj => $self->session, ); return $self->tt_process('template.tmpl', %params); }CGI::Application::Plugin::TT adds support for the popular Template Toolkit engine to your CGI::Application modules by providing several helper methods that allow you to process template files from within your runmodes.It compliments the support for HTML::Template that is built into CGI::Application through the load_tmpl method. It also provides a few extra features than just the ability to load a template.METHODStt_processThis is a simple wrapper around the Template Toolkit process method. It accepts zero, one or two parameters; an optional template filename, and an optional hashref of template parameters (the template filename is optional, and will be autogenerated by a call to $self->tt_template_name if not provided). The return value will be a scalar reference to the output of the template. package My::App::Browser sub myrunmode { my $self = shift; return $self->tt_process( 'Browser/myrunmode.tmpl', { foo => 'bar' } ); } sub myrunmode2 { my $self = shift; return $self->tt_process( { foo => 'bar' } ); # will process template 'My/App/Browser/myrunmode2.tmpl' }tt_configThis method can be used to customize the functionality of the CGI::Application::Plugin::TT module, and the Template Toolkit module that it wraps. The recommended place to call tt_config is as a class method in the global scope of your module (See SINGLETON SUPPORT for an explanation of why this is a good idea). If this method is called after a call to tt_process or tt_obj, then it will die with an error message.It is not a requirement to call this method, as the module will work without any configuration. However, most will find it useful to set at least a path to the location of the template files ( or you can set the path later using the tt_include_path method). our $TEMPLATE_OPTIONS = { COMPILE_DIR => '/tmp/tt_cache', DEFAULT => 'notfound.tmpl', PRE_PROCESS => 'defaults.tmpl', }; __PACKAGE__->tt_config( TEMPLATE_OPTIONS => $TEMPLATE_OPTIONS );The following parameters are accepted:TEMPLATE_OPTIONS This allows you to customize how the Template object is created by providing a list of options that will be passed to the Template constructor. Please see the documentation for the Template module for the exact syntax of the parameters, or see below for an example.TEMPLATE_NAME_GENERATOR This allows you to provide your own method for auto-generating the template filename. It requires a reference to a function that will be passed the $self object as it's only parameter. This function will be called everytime $self->tt_process is called without providing the filename of the template to process. This can standardize the way templates are organized and structured by making the template filenames follow a predefined pattern. The default template filename generator uses the current module name, and the name of the calling function to generate a filename. This means your templates are named by a combination of the module name, and the runmode.TEMPLATE_PRECOMPILE_DIR This options allows you to specify a directory (or an array of directories) to search when this module is loaded and then compile all files found into memory. This provides a speed boost in persistant environments (mod_perl, fast-cgi) and can improve memory usage in environments that use shared memory (mod_perl).TEMPLATE_PRECOMPILE_FILETEST This option allows you to specify exactly which files will get compiled when using the TEMPLATE_PRECOMPILE_DIR option. You can provide it with one of 3 different variable types: STRING A filename extension that can specify what type of files will be loaded (eg 'tmpl'). REGEXP Filenames that match the regular expression will be precompiled ( eg qr/.(tt|tmpl|html)$/ ). CODEREF A code reference that will be called once for each filename and directory found, and if it returns true, the template will be precompiled (eg sub { my $file = shift; ... } ).tt_objThis method will return the underlying Template Toolkit object that is used behind the scenes. It is usually not necesary to use this object directly, as you can process templates and configure the Template object through the tt_process and tt_config methods. Every call to this method will return the same object during a single request.It may be useful for debugging purposes.tt_paramsThis method will accept a hash or hashref of parameters that will be included in the processing of every call to tt_process. It is important to note that the parameters defined using tt_params will be passed to every template that is processed during a given request cycle. Usually only one template is processed per request, but it is entirely possible to call tt_process multiple times with different templates. Everytime tt_process is called, the hashref of parameters passed to tt_process will be merged with the parameters set using the tt_params method. Parameters passed through tt_process will have precidence in case of duplicate parameters.This can be useful to add global values to your templates, for example passing the user's name automatically if they are logged in. sub cgiapp_prerun { my $self = shift; $self->tt_params(username => $ENV{REMOTE_USER}) if $ENV{REMOTE_USER}; }tt_clear_paramsThis method will clear all the currently stored parameters that have been set with tt_params.tt_pre_processThis is an overridable method that works in the spirit of cgiapp_prerun. The method will be called just before a template is processed, and will be passed the template filename, and a hashref of template parameters. It can be used to make last minute changes to the template, or the parameters before the template is processed. sub tt_pre_process { my ($self, $file, $vars) = @_; $vars->{user} = $ENV{REMOTE_USER}; return; }If you are using CGI::Application 4.0 or greater, you can also register this as a callback. __PACKAGE__->add_callback('tt_pre_process', sub { my ($self, $file, $vars) = @_; $vars->{user} = $ENV{REMOTE_USER}; return; });tt_post_processThis, like it's counterpart cgiapp_postrun, is called right after a template has been processed. It will be passed a scalar reference to the processed template. sub tt_post_process { my ($self, $htmlref) = shift; require HTML::Clean; my $h = HTML::Clean->new($htmlref); $h->strip; my $newref = $h->data; $$htmlref = $$newref; return; }If you are using CGI::Application 4.0 or greater, you can also register this as a callback (See tt_pre_process for an example of how to use it).tt_template_nameThis method will generate a template name for you based on two pieces of information: the method name of the caller, and the package name of the caller. It allows you to consistently name your templates based on a directory hierarchy and naming scheme defined by the structure of the code. This can simplify development and lead to more consistent, readable code.If you do not want the template to be named after the method that called tt_template_name, you can pass in an integer, and the method used to generate the template name will be that many levels above the caller. It defaults to zero.For example: package My::App::Browser sub dummy_call { my $self = shift; return $self->tt_template_name(1); # parent callers name } sub view { my $self = shift; my $template; $template = $self->tt_template_name; # returns 'My/App/Browser/view.tmpl' $template = $self->dummy_call; # also returns 'My/App/Browser/view.tmpl' return $self->tt_process($template, { var1 => param1 }); }To simplify things even more, tt_process automatically calls $self->tt_template_name for you if you do not pass a template name, so the above can be reduced to this: package MyApp::Example sub view { my $self = shift; return $self->tt_process({ var1 => param1 }); # process template 'MyApp/Example/view.tmpl' }Since the path is generated based on the name of the module, you could place all of your templates in the same directory as your perl modules, and then pass @INC as your INCLUDE_PATH parameter. Whether that is actually a good idea is left up to the reader. $self->tt_include_path(@INC);tt_include_pathThis method will allow you to set the include path for the Template Toolkit object after the object has already been created. Normally you set the INCLUDE_PATH option when creating the Template Toolkit object, but sometimes it can be useful to change this value after the object has already been created. This method will allow you to do that without needing to create an entirely new Template Toolkit object. This can be especially handy when using the Singleton support mentioned below, where a Template Toolkit object may persist across many request. It is important to note that a call to tt_include_path will change the INCLUDE_PATH for all subsequent calls to this object, until tt_include_path is called again. So if you change the INCLUDE_PATH based on the user that is connecting to your site, then make sure you call tt_include_path on every request. my $root = '/var/www/'; $self->tt_include_path( );When called with no parameters tt_include_path returns an arrayref containing the current INCLUDE_PATH.


CGI::Application::Plugin::TT Related Software