NAME
Text::MicroTemplate
SYNOPSIS
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);
DESCRIPTION
Text::MicroTemplate is a standalone, fast, intelligent, extensible template engine with following features.
standalone
Text::MicroTemplate does not rely on other CPAN modules.
fast
Based on Mojo::Template
, expressions in the template is perl code.
intelligent
Text::MicroTemplate automatically escapes variables when and only when necessary.
extensible
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).
TEMPLATE SYNTAX
# 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>
EXPORTABLE FUNCTIONS
as_html($template)
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 $@;
raw_string($str)
wraps given string to an object that will not be escaped by the template engine
OO-STYLE INTERFACE
Text::MicroTemplate provides OO-style interface to handle more complex cases. The constructor accepts a hash (or a hasref) with following arguments.
template
template
escape_func
name of function used to escape variables. If set to undef, no escape occurs. (default: html escape)
OO-STYLE ACCESSORS
code()
returns perl code that renders the template when evaluated
AUTHOR
Kazuho Oku <kazuhooku gmail.com>
Tokuhiro Matsuno <tokuhirom AAJKLFJEF GMAIL COM>
The module is based on Mojo::Template by Sebastian Riedel.
LICENSE
This program is free software, you can redistribute it and/or modify it under the same terms as Perl 5.10.