NAME

Text::MicroTemplate

SYNOPSIS

use Text::MicroTemplate qw(:all);

# simple form
$html = render_mt(
     'hello, <?= $args->{user} ?>',
     { user => 'John' },
)->as_string;

# cache compiled template as subref
$renderer = build_mt('hello, <?= $args->{user} ?>');
$html = $renderer->({ user => 'John' })->as_string;

# complex form
$mt = Text::MicroTemplate->new(
    template => 'hello, <?= $query->param('user') ?>,
);
$code = $mt->code;
$renderer = eval << "..." or die $@;
sub {
    my \$query = shift;
    $code->();
}
...
$html = $renderer->(CGI->new)->as_string;

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

render_mt($template, $args)

Utility function that renders given template and returns an EncodedString.

# render
$hello = render_mt(
    'hello, <?= $args->{user} ?>!',
    { user => 'John' },
);

# print as HTML
print $hello->as_string;

# use the result in another template (no double-escapes)
$enc = render_mt(
    '<h1><?= $args->{hello} ?></h1>',
    { hello => $hello },
);

Intertally, the function is equivalent to:

build_mt($template)->($args);

build_mt($template)

Returns a subref that renders given template. Parameters are equivalent to Text::MicroTemplate->new.

# build template renderer at startup time and use it multiple times
my $renderer = build_mt('hello, <?= $args->{user} ?>!');

sub run {
    ...
    my $hello = $renderer->({ user => $query->param('user') });
    ...
}

encoded_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.

new($template)

new({ template => $template, escape_func => 'escape_funcname' })

In the first form, constructs a HTML template renderer. In the second form, constructs a renderer that would escape non-encoded strings using given escape function. If escape function is set to undef, no escape will occur. The default escape function is Text::MicroTemplate::escape_html.

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.