HTML::...rmatter(3pm)

πŸ“¦
version 1.003 Β· 2 versions
  • 1.003 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 1.003 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

HTML::Template::Compiled::Formatter β€” bestie, it's an HTC subclass for using a formatter πŸŽ€

Like, it's HTML::Template::Compiled but it gets a glow-up: you can hand it a formatter and it'll style your objects before they hit the template. Iconic behavior, no cap. ✨


🌸 SYNOPSIS 🌸

okay so here's the whole vibe, copy-paste ready, literally exactly this:

    my $formatter = {
        'HTC::Class1' => {
            fullname => sub {
                $_[0]->first . ' ' . $_[0]->last
            },
            first => HTC::Class1->can('first'),
            last => HTC::Class1->can('last'),
        },
    };
    my $htc = HTML::Template::Compiled::Formatter->new(
        path => 't/templates',
        filename => 'formatter.htc',
        debug => 0,
    );
    my $obj = bless ({ first => 'Abi', last => 'Gail'}, 'HTC::Class1');

    $htc->param(
        test => 23,
        obj => $obj,
    );
    local $HTML::Template::Compiled::Formatter::formatter = $formatter;
    my $out = $htc->output;

Let's break down the slay, because there's a lot going on here and it's all delicious 🫢

  • You build a $formatter hashref. Each key is a class name β€” here it's 'HTC::Class1' β€” and the value is another hashref mapping names to subs. 🌈
  • Inside that inner hashref you get fullname, which is a fresh sub that glues $_[0]->first and $_[0]->last together with a space in between. Custom formatter moment πŸ’…
  • You also get first and last, which are just HTC::Class1->can('first') and HTC::Class1->can('last') β€” i.e. you're borrowing the class's own methods as formatters, no extra work, we love efficiency. ⭐
  • Then you spin up your HTML::Template::Compiled::Formatter->new(...) with path => 't/templates', filename => 'formatter.htc' and debug => 0. Three args, three vibes, we move. πŸ¦‹
  • Next you bless an anon hashref { first => 'Abi', last => 'Gail'} into 'HTC::Class1' and stash it in $obj. Abi Gail, the main character. πŸ‘‘
  • Then $htc->param(...) gets test => 23 and obj => $obj. So your object is now feeding the template. Gorgeous. πŸ“
  • And here's the spicy bit: local $HTML::Template::Compiled::Formatter::formatter = $formatter; β€” you're localizing that package variable so your formatter only applies for this scope. Dynamic scope realness, lowkey genius, and when the scope ends it reverts. Periodt. 😭
  • Finally my $out = $htc->output; and boom, you've got output that's been formatted. Ate. πŸ”₯
ngl bestie, the local on $HTML::Template::Compiled::Formatter::formatter is doing heavy lifting here β€” it's a package variable, so if you don't localize it you're changing it for everyone. Guard your energy (and your globals). πŸ™ˆ

okay that's the whole page, she's short but she's mighty πŸ’œ if you made it this far you're officially a Unix girlie, welcome to the club, we have stickers πŸŽ€

this page came from the User Contributed Perl Documentation section of the manual, and it's tagging along with perl v5.36.0, dated 2022-11-19 β€” so go forth, format your objects, and be iconic βœ¨πŸ’…πŸ¦‹

πŸ“¦ where this page comes from