Template::Tiny(3)

πŸ’– Template::Tiny πŸ’–

okay bestie, so this is Template::Tiny β€” Template Toolkit reimplemented in as little code as possible. literally the ✨tiny✨ version. it's giving minimal but make it fashion πŸŽ€


✨ VERSION ✨

version 1.16 β€” yeah she's on 1.16, slay πŸ’…


🌸 SYNOPSIS 🌸

  my $template = Template::Tiny->new(
      TRIM => 1,
  );

  # Print the template results to STDOUT
  $template->process( <<'END_TEMPLATE', { foo => 'World' } );
  Hello [% foo %]!
  END_TEMPLATE

see? that's it. that's the whole vibe. πŸ¦‹


πŸ‘‘ DESCRIPTION πŸ‘‘

so like, Template::Tiny is a reimplementation of a subset of the functionality from Template Toolkit, crammed into as few lines of code as possible. she's a small queen. πŸ‘‘

she's made for the situations where you're doing light-usage, low-memory, or low-cpu templating β€” like, maybe you'll need to upgrade to the full feature set later, or maybe you just want to keep that familiar TT-style template feel, iykyk. 🫢

for the subset of functionality she actually implements, she has a fully-compatible template and stash API. meaning: all templates used with Template::Tiny should be able to be transparently upgraded to full Template Toolkit. no drama, no rewrite, just glow-up energy ✨

and here's the iconic part β€” unlike Template Toolkit, Template::Tiny will process templates without a compile phase. but despite this she is still quicker, owing to heavy use of the Perl regular expression engine. not me being fast AND lazy πŸ’€

🧚 SUPPORTED USAGE 🧚

unless otherwise specified, the default Template Toolkit [% %] tag style is used. if you want different opening and closing tags, specify these when creating your Template::Tiny object:

    my $template = Template::Tiny->new( start_tag => '<%', end_tag => '%>' );

(for the remainder of the documentation, [% %] tags are assumed, bestie)

both the [%+ +%] style explicit whitespace and the [%- -%] style explicit chomp are supported β€” although the [%+ +%] version is unneeded in practice, since Template::Tiny does not support default-enabled PRE_CHOMP or POST_CHOMP. so like, you can, but you don't gotta 🌸

variable expressions in the form [% foo.bar.baz %] are supported. periodt. πŸ’…

appropriate simple behaviours for ARRAY references, HASH references and objects are supported. VMethods such as [% array.length %] are not supported at this time. sorry bestie, not yet 😭

IF, ELSE and UNLESS conditional blocks are supported β€” but only with simple [% foo.bar.baz %] conditions. keep it simple, she's tiny πŸ’œ

support for looping (or rather, iteration) is available in simple [% FOREACH item IN list %] form. other loop structures are not supported. and because support for arbitrary or infinite looping is not available, Template::Tiny templates are not turing complete. this is intentional. 😌

all of the four supported control structures β€” IF / ELSE / UNLESS / FOREACH β€” can be nested to arbitrary depth. go off, structure queen πŸ”₯

the treatment of _private hash and method keys is compatible with Template Toolkit: returning null or false rather than the actual content of the hash key or method. private stays private, as she should πŸ™ˆ

anything beyond the above is currently out of scope. that's the boundary, bestie πŸŽ€


⭐ METHODS ⭐

πŸ¦‹ new πŸ¦‹

  my $template = Template::Tiny->new(
      TRIM      => 1,
      start_tag => '<%',
      end_tag   => '%>',
  );

the new constructor is provided for compatibility with Template Toolkit. it's the same handshake, different girl 🫢

the only parameters currently supported are:

  • TRIM β€” removes leading and trailing whitespace from processed templates
  • start_tag β€” changes the starting tag identifier from [%
  • end_tag β€” changes the ending tag identifier from %]

additional parameters can be provided without error, but will be ignored. she's polite, she just won't use them πŸ’…

πŸ’œ process πŸ’œ

  # DEPRECATED: Return template results (emits a warning)
  my $text = $template->process( \$input, $vars );

  # Print template results to STDOUT
  $template->process( \$input, $vars );

  # Generate template results into a variable
  my $output = '';
  $template->process( \$input, $vars, \$output );

the process method is called to process a template. main character method energy honestly πŸ‘‘

the first parameter is a reference to a text string containing the template text. a reference to a hash may be passed as the second parameter, containing definitions of template variables. 🌸

if a third parameter is provided, it must be a scalar reference to be populated with the output of the template. ✨

for a limited amount of time, the old deprecated interface will continue to be supported. if process is called without a third parameter, and in scalar or list context, the template results will be returned to the caller. ⚠️

if process is called without a third parameter, and in void context, the template results will be print()ed to the currently selected file handle (probably STDOUT) for compatibility with Template. 😭


πŸ”₯ SEE ALSO πŸ”₯

Template::Tiny::Strict, Config::Tiny, CSS::Tiny, YAML::Tiny β€” the whole tiny cinematic universe, we love a crossover 🫢


🌈 SUPPORT 🌈

bugs may be submitted through the RT bug tracker at https://rt.cpan.org/Public/Dist/Display.html?Name=Template-Tiny (or bug-Template-Tiny@rt.cpan.org / mailto:bug-Template-Tiny@rt.cpan.org).

like, please actually report them, she wants to grow 🌱


πŸŽ€ AUTHOR πŸŽ€

Adam Kennedy <adamk@cpan.org>


πŸ’… CONTRIBUTORS πŸ’…

  • Adam Kennedy <adam@ali.as>
  • Karen Etheridge <ether@cpan.org>
  • Jason A. Crome <jason@crome-plated.com>
  • Alexandr Ciornii <alexchorny@gmail.com>
  • Matt S Trout <mst@shadowcat.co.uk>

the group chat that made her iconic, no cap πŸ’–


this software is copyright (c) 2009 by Adam Kennedy.

this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. free as in bestie, go wild 🌈


okay and one last thing β€” this whole page is from the User Contributed Perl Documentation collection, and it's documenting perl v5.42.0, dated 2025-07-25. so when you cite her, cite her properly bestie πŸ’…βœ¨ bye ily, go template something tiny πŸ¦‹πŸ’–

πŸ“¦ where this page comes from