Templa...scaping(3pm)
π Templa...scaping(3pm) π
okay bestie, welcome to the User Contributed Perl Documentation section, where this little page lives under the name Templa...scaping(3pm). buckle up, we're talking auto-escaping in Template-Toolkit and it's honestly iconic.
β¨ NAME β¨
Template::Stash::AutoEscaping β escape automatically in Template-Toolkit. no cap, that's literally the whole job.
π¦ SYNOPSIS π¦
use Template;
use Template::Stash::AutoEscaping;
my $tt = Template->new({
STASH => Template::Stash::AutoEscaping->new
});
look at her go π
two use lines and a STASH and you're protected.
π METHODS π
πΈ new πΈ
she's the constructor, she takes a hashref of options, and here's the full menu:
die_on_unescapedβ if you set this to a true value, the process throws an exception the moment it bumps into a value that wasn't explicitly set to be escaped, or that was marked as a raw value. drama queen behaviour, but for safety π«Άescape_typeβ default isHTML.method_for_escapeβ the default method used to escape a value explicitly. mostly useful alongside"die_on_unescaped".method_for_rawβ default israw. so you can pull the not-escaped value out with[% value.raw %].escape_methodβ here's how you hand her your own escaping logic:
my $tt = Template->new({
STASH => Template::Stash::AutoEscaping->new({
escape_method => sub { my $text = shift; ... ; return $text }
})
});
ignore_escapeβ the escape-off switch, for when you need it:
my $stash = Template::Stash::AutoEscaping->new({ignore_escape => [qw(include_html include_raw my_escape_func)], ... );
You can disable auto-escape for some value or TT-Macro. For example: include another component, for output-safety html, using another escape method, etc. etc. the list goes on β¨
π class_for π
Template::Stash::AutoEscaping->class_for("HTML") # Template::Stash::AutoEscaping::Escaped::HTML
Template::Stash::AutoEscaping->class_for("HTML" => "MyHTMLString");
two ways to call her, same energy.
π§ escape π§
For internal use. (iykyk, don't touch π)
π§ escape_count π§
For internal use.
π§ get π§
For internal use.
π§ get_raw_args π§
For internal use.
π₯ DESCRIPTION π₯
Template::Stash::AutoEscaping is a sub class of Template::Stash that automatically escapes all HTML strings and avoids XSS vulnerability. that's it, that's the vibe β your templates stay cute and safe π
β CONFIGURE β
$Template::Stash::AutoEscaping::ESCAPE_ARGSβ default is0. for example"key of hash"or"args of vmethods"are not escaped. I think this is good in most cases. so,[% hash.${key} %] [% hash.item(key) %]means[% hash.${key.raw} | html %] [% hash.item(key.raw) | html %]by default. periodt.
π AUTHOR π
- mala
<cpan@ma.la>β original author ofTemplate::Stash::AutoEscape - Shlomi Fish (
<http://www.shlomifish.org/>) β added some enhancements and fixes, while disclaiming all rights, as part of his work for<http://reask.com/>and released the result as"Template::Stash::AutoEscaping".
π SEE ALSO π
TemplateTemplate::Stash::EscapedHTMLTemplate::Stash::AutoEscape
go read those too, bestie, they're all part of the fam π«Ά
πΈ LICENSE πΈ
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's a wrap π this page shipped as part of perl v5.34.0, dated 2022-06-17, filed under Templa...scaping(3pm). slay, go escape some HTML, ily πβ¨
π¦ where this page comes from
- debian bookworm: libtemplate-stash-autoescaping-perl 0.0303-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtemplate-stash-autoescaping-perl 0.0303-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtemplate-stash-autoescaping-perl 0.0303-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtemplate-stash-autoescaping-perl 0.0303-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtemplate-stash-autoescaping-perl 0.0303-2 Β· 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