Text::MicroTemplate
use Text::MicroTemplate qw(as_html); # simple form $user = 'John'; $html = eval as_html('hello, <?= $user ?>') or die $@; # complex form $mt = Text::MicroTemplate->new( template => 'hello, <?= $query->param('user') ?>, ); $code = $mt->code; $builder = eval << "..." or die $@; sub { my \$query = shift; $code->(); } ... $html = $builder->(CGI->new);
Text::MicroTemplate is a standalone, fast, intelligent, extensible template engine with following features.
Text::MicroTemplate does not rely on other CPAN modules.
Based on Mojo::Template, expressions in the template is perl code.
Mojo::Template
Text::MicroTemplate automatically escapes variables when and only when necessary.
Text::MicroTemplate does not provide features like template cache or including other files by itself. However, it is easy to add you own (that suites the most to your application), by wrapping the result of the module (which is a perl expression).
# output the result of expression with automatic escape <?= $expr ?> (tag style) ?= $expr (per-line) # output the result expression without escape (tag style) <?=r $raw_str ?> ?=r $raw_str # execute perl code (tag style) <? foo() ?> ? foo() # comment (tag style) <?# comment ?> ?# comment # loops <ul> ? for my $item (@list) { <li><?= $item ?></li> ? } </ul>
Utility funtion that returns an expression that renders given template when evaluated.
# outputs: hello, John! $user = 'John'; $html = eval as_html('hello, <?= $user ?>!') or die $@;
wraps given string to an object that will not be escaped by the template engine
Text::MicroTemplate provides OO-style interface to handle more complex cases. The constructor accepts a hash (or a hasref) with following arguments.
template
name of function used to escape variables. If set to undef, no escape occurs. (default: html escape)
returns perl code that renders the template when evaluated
Kazuho Oku <kazuhooku gmail.com>
Tokuhiro Matsuno <tokuhirom AAJKLFJEF GMAIL COM>
The module is based on Mojo::Template by Sebastian Riedel.
This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10.
To install Text::MicroTemplate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::MicroTemplate
CPAN shell
perl -MCPAN -e shell install Text::MicroTemplate
For more information on module installation, please visit the detailed CPAN module installation guide.