NAME
PlackX::Framework::Template - Use templates in a PlackX::Framework app.
SYNOPSIS
This module allows a convenient way to select template files, add parameters, and process and output the templates. By default, the Template Toolkit module ('Template') is used as the engine, but you can use your own.
If using Template Toolkit, you can pass options to Template->new by including a hashref in your use statement:
# Your app
package MyApp {
use PlackX::Framework; # (automatically generates MyApp::Template...)
use MyApp::Template { OPTION => 'value', ... };
}
Your PlackX::Framework app will automatically create a new instance of this class and make it available to your $response object.
# In your controller
my $template = $response->template;
$template->set_filename('foobar.tmpl'); # or ->use('foobar.tmpl');
$template->add_params(food => 'pizza', drink => 'beer'); # or ->set(...)
return $template->render;
CUSTOM TEMPLATE ENGINE
To use your own, subclass this module and override get_engine() method to return an instance of your templating engine object. If your engine does not have a Template Toolkit-like process() method, you will have to override the output() method of this module as well.
This example assumes you reuse the same template object for each request:
package MyApp::Template {
my $templater = Some::Template::Engine->new;
sub get_engine { $templater; }
sub output ($self, $file) {
$self->get_engine->render($file, $self->{'params'}->%*);
}
}
However you can also create a new one for each request:
package MyApp::Template {
sub get_engine { Some::Template::Engine->new() }
sub output ($self, $file) {
$self->get_engine->render($file, $self->{'params'}->%*);
}
}
As an example, consider an extremely simple template engine that simply replaces {{variable}} with the value of key 'variable' in the params:
package MyTemplateEngine {
sub new { bless {}, shift }
sub process ($self, $file, $params, $response) {
my $content = readfile($file); # readfile() implementation not shown
foreach my ($key, $val) (%$params) {
$content =~ s`\{\{$key\}\}`$val`g;
}
$response->print($content);
}
}
Which we can use our new engine in our PlackX::Framework app like this:
package MyApp::Template {
use parent 'PlackX::Framework::Template';
sub get_engine { MyTemplateEngine->new() }
}
In this case, there is no need to override output() as our engine's process() method is TT-like.
CLASS METHODS
new($response) =head2 new($response, $engine)
Create a new instance of this class, optionally specifying the template engine. If not specified, get_engine() will be called.
The first argument, a PlackX::Framework response object, is required.
get_engine
Get the Template engine object (e.g., the Template Toolkit instance).
set_engine($obj)
Set the Template engine object (e.g., the Template Toolkit instance).
OBJECT METHODS
get_param($key)
Get the value of a template parameter.
set_params($k => $v, $k2 => $v2...) =head2 set(...)
Set the value of template parameters. Aliased as set() for short.
set_filename($filename) =head2 use(...)
Set the template filename to be automatically passed to output(). Aliased as use() for short.
output =head2 output($filename)
Process the template file, to optionally include the filename which will override any previous calls to set_filename() or use().
render(...)
Call the output(...) method, passing the same arguments, and return the Plack response object.