NAME Catalyst::View::Tenjin - Tenjin view class for Catalyst. VERSION version 0.042 SYNOPSIS # create your view script/myapp_create.pl view Tenjin Tenjin # check your new view's configuration __PACKAGE__->config( USE_STRICT => 1, INCLUDE_PATH => [ MyApp->path_to('root', 'templates') ], TEMPLATE_EXTENSION => '.html', ENCODING => 'utf8', ); # render view from lib/MyApp.pm or lib/MyApp::C::SomeController.pm sub message : Global { my ($self, $c) = @_; $c->stash->{template} = 'message.html'; $c->stash->{message} = 'Hello World!'; $c->forward('MyApp::View::Tenjin'); } # 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 =] # you can also embed Perl action->namespace eq 'admin') { ?>

admin is not implemented yet

DESCRIPTION This is the Catalyst view class for the Tenjin template engine. Your application should define a view class which is a subclass of this module. There is no helper script to create this class automatically, but you can do so easily as described in the synopsis. Once you've created the view class, 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 Tenjin view class. # In MyApp or MyApp::Controller::SomeController sub end : Private { my( $self, $c ) = @_; $c->forward('MyApp::View::Tenjin'); } This module is now Moose-based, so you can use method modifiers. For example, you can perform some operation after or before this module begins processing the request or rendering the template. METHODS COMPONENT( $c, $arguments ) This method is automatically called by Catalyst when creating the view. The method creates an instance of Tenjin using the configuration options set in the view. process() Renders the template specified in "$c->stash->{template}" or "$c->action" (the private name of the matched action, with the default extension specified by the "TEMPLATE_EXTENSION" configuration item. Calls render to perform actual rendering. Output is stored in "$c->response->body". check_tmpl( $template_name ) Checks if a template named $template_name was already registered with the view. Returns 1 if yes, "undef" if no. register( $tmpl_name, $tmpl_content ) Registers a template with the view from an arbitrary source, for immediate usage in the application. $tmpl_name is the name of the template, used to distinguish it from others. $tmpl_content is the body of the template. Templates are registered in memory, so don't expect them to remain registered between application restarts. render( $c, $template, \%args ) Renders the given template and returns output, or throws an exception if an error was encountered. $template is the name of the template you wish to render. If this template was not registered with the view yet, it will be searched for in the directories set in the "INCLUDE_PATH" configuration item. 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", $name to "$c->config->{name}" and the Catalyst context, which will be set to $c unless the "CATALYST_VAR" configuration item is set to a different name. If so, the $c, $base and $name variables are omitted. template_vars( $c ) Returns a list of key-value pairs to be used as the context variables (i.e. the context object) in the Tenjin templates. _coerce_paths( $dlim ) CONFIGURATION To configure your view class, you can call the "config()" method in the view subclass. This happens when the module is first loaded. package MyApp::View::Tenjin; use strict; use base 'Catalyst::View::Tenjin'; __PACKAGE__->config( USE_STRICT => 1, INCLUDE_PATH => [ MyApp->path_to('root', 'templates') ], TEMPLATE_EXTENSION => '.html', ENCODING => 'utf8', ); You can also define a class item in your main application configuration, again by calling the uniquitous "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 "MRO::Compat" if you redefine the "new()" method in a subclass). package MyApp; use strict; use Catalyst; MyApp->config({ name => 'MyApp', root => MyApp->path_to('root'), 'View::Tenjin' => { USE_STRICT => 1, INCLUDE_PATH => [ MyApp->path_to('root', 'templates') ], TEMPLATE_EXTENSION => '.html', ENCODING => 'utf8', }, }); The "USE_STRICT" configuration option determines if Tenjin will "use strict" when evaluating the embedded Perl code inside your templates. If "USE_STRICT" is set to a true value (1), strict will be used. This is recommended, but if you're having trouble using "strict", you can set it to 0, or just not set it at all (by default, Tenjin will not "use strict" on embedded Perl code). The "ENCODING" configuration option tells Tenjin that how your template files are encoded. By default, Tenjin will try to decode your templates as utf8. If you set "TEMPLATE_EXTENSION", this extension will be automatically appended to "<$c-"stash->{template}>> before being searched in the "INCLUDE_PATH". 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('Tenjin')->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('Tenjin')->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. 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.html'; $c->forward('MyApp::View::Tenjin'); } 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 '.html', it would load "root/message.html". The items defined in the stash are passed to Tenjin for use as template variables. sub default : Private { my ($self, $c) = @_; $c->stash->{template} = 'message.html'; $c->stash->{message} = 'Hello World!'; $c->forward('MyApp::View::Tenjin'); } 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.html The message is: [== $message =] The base is [== $base =] The name is [== $name =] The output generated by the template is stored in "$c->response->body". MANUALLY PROVIDING TEMPLATES Catalyst::View::Tenjin adds an easy method for providing your own templates, such that you do not have to use template files stored on the file system. For example, you can use templates stored on a DBIx::Class schema. This is similar to Template Toolkit's provider modules, which for some reason I never managed to get working. You can register templates with your application, and use them on the fly. For example: # check if the template was already registered unless ($c->view('Tenjin')->check_tmpl($template_name)) { # Load the template my $tmpl = $c->model('DB::Templates')->find($template_name); $c->view('Tenjin')->register($template_name, $tmpl->content); } CAPTURING TEMPLATE OUTPUT If you wish to use the output of a template for some other purpose than displaying in the response, you can use the render method. For example, use can use it with Catalyst::Plugin::Email: sub send_email : Local { my ($self, $c) = @_; $c->email( header => [ To => 'me@localhost', Subject => 'A TT Email', ], body => $c->view('Tenjin')->render($c, 'email.html', { additional_template_paths => [ $c->config->{root} . '/email_templates'], email_tmpl_param1 => 'foo' }), ); # Redirect or display a message } CHANGES * 0.04: Created a helper module so views can be created with myapp_create.pl * 0.03: Updated for compatibility with the new and revised Tenjin version. * 0.03: This module is now based on Moose. TODO * Create appropriate tests. BUGS Please report any bugs or feature requests to "bug-tenjin at rt.cpan.org", or through the web interface at . I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. SUPPORT You can find documentation for this module with the perldoc command. perldoc Catalyst::View::Tenjin You can also look for information at: * RT: CPAN's request tracker * AnnoCPAN: Annotated CPAN documentation * CPAN Ratings * Search CPAN SEE ALSO Tenjin, Catalyst, Catalyst::View::TT AUTHOR Ido Perlmuter . This module was adapted from Catalyst::View::TT, so most of the code and even the documentation belongs to the authors of Catalyst::View::TT. Development of this module is done with github at . LICENSE AND COPYRIGHT Copyright (c) 2009-2010 the aforementioned authors. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.