Text::...emplate(3pm)

πŸ“¦
version 0.24 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Text::MicroTemplate β€” bestie, it's a micro template engine, and it speaks Perl5. periodt.


🌸 SYNOPSIS 🌸

    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;

✨ DESCRIPTION ✨

okay so Text::MicroTemplate is a standalone, fast, intelligent, extensible template engine β€” a whole main character πŸ’… and here's the glow-up list:

πŸ¦‹ standalone

Text::MicroTemplate does not rely on other CPAN modules. no entanglements, no drama, she's independent πŸ’…

πŸ”₯ fast

Based on Mojo::Template, expressions in the template is perl code. so like, the template is Perl, iykyk.

🧠 intelligent

Text::MicroTemplate automatically escapes variables when and only when necessary. she reads the room, fr fr.

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

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


πŸŽ€ TEMPLATE SYNTAX πŸŽ€

The template language is Perl5 itself! not me being obsessed with a language that's also a template language 😭 iconic behavior.

    # 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>

🫢 EXPORTABLE FUNCTIONS 🫢

build_mt($template)

Returns a subref that renders given template. Parameters are equivalent to Text::MicroTemplate->new. build it once at startup and reuse it like a good little cache-free icon πŸ’–

    # 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'));
        ...
    }

render_mt($template, @args)

Utility function that combines build_mt and call to the generated template builder. the lazy-girl-approved one-liner ✨

    # 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)->(@_);

encoded_string($str)

wraps given string to an object that will not be escaped by the template engine. she's pre-marked as safe, so the escape logic lowkey leaves her alone πŸŽ€


πŸ‘‘ OO-STYLE INTERFACE πŸ‘‘

Text::MicroTemplate provides OO-style interface to handle more complex cases. for when things get situationship complicated.

new($template)

new(%args)

new(\%args)

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

  • template β€” template string (mandatory) πŸ’– like, you literally cannot skip this one bestie
  • escape_func β€” escape function (defaults to Text::MicroTemplate::escape_html), no escape when set to undef. undef means she's raw, no filter, no cap 😭
  • package_name β€” package under where the renderer is compiled (defaults to caller package)
  • prepend β€” Prepends Perl code to the template.

code()

returns perl code that renders the template when evaluated πŸ‘‘

filter(sub filter_func { ... })->(sub { template lines })

filters given template lines πŸ¦‹

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

πŸ’€ DEBUG πŸ’€

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

bestie the energy escalates: 1 = chatty, 2 = yells the compiled Perl at you via warn(), 3 = same yelling but it's die(), so she's taking everyone down with her πŸ’€

🌟 SEE ALSO 🌟

  • Text::MicroTemplate::File
  • Text::MicroTemplate::Extended

πŸ’œ 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 itself. slay, she's free, she's open, she's giving community πŸ’…βœ¨


and that's the whole vibe check, bestie 🫢 this page came to you from perl v5.36.0, dated 2022-10-13, and it's filed under Text::MicroTemplate (the header calls her Text::...emplate(3pm) because truncation is also a lifestyle 😭) β€” go compile something cute, ily πŸ’–πŸŒˆπŸŽ€

πŸ“¦ where this page comes from