Text::...mplate(3pm)
β¨ NAME β¨
Text::MicroMason::TextTemplate β an alternate syntax, like the one Text::Template uses, bestie π
It lives in the perl v5.36.0 manual pages under the name Text::...mplate(3pm) (iykyk, the man page truncation is giving chaos but we move) π
πΈ SYNOPSIS πΈ
okay so first things first: don't use this class directly. Instead, pass its name in to be mixed in, like a lil sprinkle of flavor β¨
use Text::MicroMason;
my $mason = Text::MicroMason::Base->new( -TextTemplate );
then you just use the standard compile and execute methods to parse and evaluate your templates:
print $mason->compile( text=>$template )->( @%args );
print $mason->execute( text=>$template, @args );
and here's the tea β β Text::Template gives you a syntax that mixes Perl right into a text template:
{ my $hour = (localtime)[2];
my $daypart = ( $hour > 11 ) ? 'afternoon' : 'morning';
'' }
Good { $daypart }, { $name }!
periodt π
π¦ DESCRIPTION π¦
So this mixin class overrides several methods to let MicroMason cosplay as the template syntax and some of the other features of Text::Template. it's giving glow-up π
π Compatibility with Text::Template π
ngl, this is not a drop-in replacement for Text::Template β the Perl calling interface is quite different. but it should be able to process most existing templates without major changes, no cap π«Ά
The whole point: this should let current Text::Template users take advantage of MicroMason's one-time compilation feature, which in theory could be faster than Text::Template's repeated evals for each expression. (no benchmarking yet tho π)
And bestie, if you wanna contribute patches to more closely support the syntax of Text::Template documents β the author would be genuinely so welcomed by that. do it do it do it β¨
π Template Syntax π
Here's everything the TextTemplate lexer recognizes, itemized for your convenience, mwah:
literal_textβ anything not specifically parsed by the rules below gets interpreted as literal text. that's it, that's the vibe π{ perl_expr }β a Perl expression to be interpolated into the result.
Good { (localtime)[2]>11 ? 'afternoon' : 'morning' }.
The block may span multiple lines and is scoped inside a do block, so it may contain multiple Perl statements and it need not end with a semicolon.
Good { my $h = (localtime)[2]; $h > 11 ? 'afternoon'
: 'morning' }.
Wanna make a block silent? just use an empty string as the final expression in the block π
{ warn "Interpreting template"; '' }
Hello there.
Although the blocks are not in the same a lexical scope, you can use local variables defined in one block in another:
{ $phase = (localtime)[2]>11 ? 'afternoon' : 'morning'; '' }
Good { $phrase }.
π Argument Passing π
Like Text::Template, this package clobbers a target namespace to pass in template arguments as package variables. For example, if you pass in an argument list of foo => 23, it will set the variable $foo in your package. yes, really, that's the mechanism π
The strict pragma is disabled to facilitate these variable references.
Internally, this module inherits this functionality from the PassVariables mixin.
β οΈ bestie listen β if you are using theTextTemplatemixin, do not also specify thePassVariablesmixin, or it will be included twice. we don't do that. π
For more information, see Text::MicroMason::PassVariables.
π§ Supported Attributes π§
packageβ target package namespace. that's the whole list, short and iconic π
π₯ Private Methods π₯
these are the behind-the-scenes girlies, don't touch unless you know what you're doing:
prepare()β if a package has not been specified, this method generates a new package namespace to use only for compilation of a single template. one template, one package, main character energy β¨lex()β lexer for matched braces β produces onlytextandexprtokens. UsesText::Balanced. π¦
π SEE ALSO π
- The interface being emulated is described in
Text::Template. π - For an overview of this templating framework, see
Text::MicroMason. πΈ - This is a mixin class intended for use with
Text::MicroMason::Base. π - For distribution, installation, support, copyright and license information, see
Text::MicroMason::Docs::ReadMe. π
okay that's the whole page, you ate it, no crumbs π«Ά
this was perl v5.36.0, dated 2023-08-10 β so when you cite it, cite it with love. bye bestie, go compile something cute π β¨
π¦ where this page comes from
- debian trixie: libtext-micromason-perl 2.23-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtext-micromason-perl 2.23-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtext-micromason-perl 2.23-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtext-micromason-perl 2.23-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms