GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Text::MicroTemplate(3) User Contributed Perl Documentation Text::MicroTemplate(3)

Text::MicroTemplate - Micro template engine with Perl5 language

    use Text::MicroTemplate qw(:all);

    # compile template, and render
    $renderer = build_mt('hello, <?= $_[0] ?>');
    $html = $renderer->('John')->as_string;

    # or in one line
    $html = render_mt('hello, <?= $_[0] ?>', '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;

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.

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

The module only provides basic building blocks for a template engine. Refer to Text::MicroTemplate::File for higher-level interface.

The template language is Perl5 itself!

    # output the result of expression with automatic escape
    <?= $expr ?>             (tag style)
    ?= $expr                 (per-line)

    # execute perl code (tag style)
    <? foo() ?>
    ? foo()

    # comment (tag style)
    <?# comment ?>
    ?# comment

    # loops
    <ul>
    ? for my $item (@list) {
    <li><?= $item ?></li>
    ? }
    </ul>

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, <?= $_[0] ?>!');

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

Utility function that combines build_mt and call to the generated template builder.

    # render
    $hello = render_mt('hello, <?= $_[0] ?>!', 'John');

    # print as HTML
    print $hello->as_string;

    # use the result in another template (no double-escapes)
    $enc = render_mt('<h1><?= $_[0] ?></h1>', $hello);

Internally, the function is equivalent to:

    build_mt($template)->(@_);

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.

Constructs template renderer. In the second or third form, parameters below are recognized.

template

template string (mandatory)

escape_func

escape function (defaults to Text::MicroTemplate::escape_html), no escape when set to undef

package_name

package under where the renderer is compiled (defaults to caller package)

prepend

Prepends Perl code to the template.

returns perl code that renders the template when evaluated

filters given template lines

    ? $_mt->filter(sub { s/Hello/Good bye/g })->(sub {
    Hello, John!
    ? })

The "MICRO_TEMPLATE_DEBUG" environment variable helps debugging. The value 1 extends debugging messages, 2 reports compiled Perl code with "warn()", 3 is like 2 but uses "die()".

Text::MicroTemplate::File

Text::MicroTemplate::Extended

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 itself.
2015-04-28 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.