#!/path/to/perl -w use strict; use Template; # create a template processor my $tproc = Template->new({ INCLUDE_PATH => '/user/abw/templates', # template search path }); # define variables for use in templates my $vars = { 'animal' => 'cat', 'place' => 'mat', 'list' => [ 'foo', 'bar', 'baz' ], 'user' => { 'name' => 'Me, Myself, I', 'email' => 'me@here.com' }, }; # process a template file, output defaults to STDOUT $tproc->process('myfile', $vars) || die $tproc->error(), "\n";
myfile:
[% INCLUDE header title = 'Hello World!' %] The [% animal %] sat on the [% place %] <a href="mailto:[% user.email %]">[% user.name %]</a> <ul> [% FOREACH item = list %] <li>[% item %] [% END %] </ul>
Output:
<!-- ...output from processing 'header' template... --> The cat sat on the mat <a href="mailto:me@here.com">Me, Myself, I</a> <ul> <li>foo <li>bar <li>baz </ul>
Apache/mod_perl handler:
sub handler { my $r = shift; my $file = $r->path_info(); # or some other mapping my $template = Template->new({ INCLUDE_PATH => '/where/to/look/for/templates', OUTPUT => $r, }); $template->process($file) || do { $r->log_reason($template->error()); return SERVER_ERROR; }; return OK; }
To install Template, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Template
CPAN shell
perl -MCPAN -e shell install Template
For more information on module installation, please visit the detailed CPAN module installation guide.