Text::...mplate(3pm)

πŸ“¦
version 2.23 Β· 3 versions
, in debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ 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 the TextTemplate mixin, do not also specify the PassVariables mixin, 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 only text and expr tokens. Uses Text::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