Text::...mplate(3pm)
π NAME π
Say hi to Text::MicroMason::HTMLTemplate β it's the alternate syntax that gives you HTML::Templateβstyle vibes. π¦
(also, quick vibe check: this is a perl 3pm manual page from the User Contributed Perl Documentation, so yeah bestie you're in exactly the right place π )
β¨ SYNOPSIS β¨
Lowkey important: don't use this class directly!! Pass its name to be mixed in instead. π
use Text::MicroMason;
my $mason = Text::MicroMason::Base->new( -HTMLTemplate );
Then you use the standard compile and execute methods to parse and evaluate templates:
print $mason->compile( text=>$template )->( @%args );
print $mason->execute( text=>$template, @args );
Orrr you can use HTML::Template's calling conventions:
$template = Text::MicroMason->new( -HTMLTemplate, filename=>'simple.tmpl' );
$template->param( %arguments );
print $template->output();
HTML::Template gives you a whole syntax for embedding values into a text template, and it looks like this:
<TMPL_IF NAME="user_is_dave">
I'm sorry <TMPLVAR NAME="name">, I'm afraid I can't do that right now.
<TMPL_ELSE>
<TMPL_IF NAME="daytime_is_morning">
Good morning, <TMPLVAR NAME="name">!
<TMPL_ELSE>
Good afternoon, <TMPLVAR NAME="name">!
</TMPL_IF>
</TMPL_IF>
(iconic template honestly, no notes π)
πΈ DESCRIPTION πΈ
So this mixin class overrides several methods so that MicroMason can emulate the template syntax of HTML::Template, plus some of its other features too. Main character energy, periodt. π
It also automatically includes these other mixins for you, no extra work required: TemplateDir, HasParams, and StoreOne. π«Ά
π« Compatibility with HTML::Template π«
Heads up bestie: this is not a drop-in replacement for HTML::Template, because the implementation is quite different. But it should be able to process most existing templates without major changes. β¨
The whole point is that current HTML::Template users get to take advantage of MicroMason's one-time compilation feature, which in theory could be faster than HTML::Template's run-time interpretation. (No benchmarking yet though, so don't quote me on that. π)
These HTML::Template features are not supported yet:
- Search path for files. (Candidate for a separate mixin class, or an addition to
TemplateDir.) - Many HTML::Template options are either unsupported or have different names, and they need to be mapped to equivalent sets of attributes. (Transform these in the
new()method, orcroakif they're unsupported.)
And these HTML::Template features will likely never be supported, because the implementations are just fundamentally different:
query()method
Contributed patches to more closely support the behavior of HTML::Template would be welcomed by the author. (Be the change you wanna see in the world, fr fr. π«Ά)
π¦ Template Syntax π¦
Here's everything the HTMLTemplate lexer recognizes:
literal_textβ anything not specifically parsed by the rule below is interpreted as literal text.<TMPL_tagname>β a template tag with no attributes.<TMPL_tagname varname>β a template tag with a name attribute.<TMPL_tagname NAME=varname option=value ...>β a template tag with one or more attributes.</TMPL_tagname>β a closing template tag.
And here's the tag lineup the HTMLTemplate assembler supports:
tmpl_varβ<tmpl_var name=... ( default=... ) ( escape=... ) >tmpl_includeβ<tmpl_include name=... >tmpl_ifβ<tmpl_if name=... > ... </tmpl_if>tmpl_unlessβ<tmpl_unless name=...> ... </tmpl_unless>tmpl_elseβ<tmpl_else>tmpl_loopβ<tmpl_loop name=...> ... </tmpl_loop>
π Supported Attributes π
associateβ an optional reference to a CGI parameter object, or to any other object with a similarparam()method.loop_global_vars(that's HTML::Template's"global_vars", iykyk) β if set to true, don't hide external parameters inside a loop scope.loop_context_varsβ if set to true, it defines extra variables within each<TMPL_LOOP>:__counter__, which specifies the row index, plus four boolean flags:__odd__,__first__,__inner__, and__last__. Obsessed. π
π Public Methods π
new()β creates a new Mason object. If afilenameparameter is supplied, the corresponding file gets compiled. β¨param()β gets and sets parameter arguments. Similar to theparam()method provided by HTML::Template and the CGI module.output()β executes the most-recently compiled template and returns the results.
Optionally it accepts a filehandle to print the results to:
$template->output( print_to => *STDOUT );
π§ Private Methods π§
lex_token
( $type, $value ) = $mason->lex_token();
Lexer for <TMPL_x> tags.
It attempts to parse a token from the template text stored in the global $_ and returns a token type and value. Returns an empty list if it's unable to parse further due to an error. π
parse_args()β lexer for arguments within a tag.assemble_tmpl_*()β these methods define the mapping from the template tags to the equivalent Perl code.filter()β used to implement theescapeoption fortmpl_var.
π SEE ALSO π
- The interface being emulated is described in
HTML::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.
and that's the whole page, bestie π this was perl v5.36.0, dated 2023-08-10, straight outta the User Contributed Perl Documentation. slay, bye ππ¦β¨
π¦ 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