Templa...Script(3pm)

πŸ“¦
version 0.02 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Template::Plugin::JavaScript πŸ’–

okay bestie, so this is a Perl module from the User Contributed Perl Documentation collection, and it's filed under section 3pm β€” that's the "Perl module" section, iykyk. It lives in the Template::Plugin::JavaScript namespace and it is literally here to make your text safe to drop inside JavaScript. No cap. ✨


🌸 NAME 🌸

Template::Plugin::JavaScript β€” encodes text so it's safe to use in JavaScript. That's the whole job. It's giving bodyguard energy for your strings. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

Here's the vibe in action:

  [% USE JavaScript %]
  <script type="text/javascript">
  document.write("[% sometext | js %]");
  </script>

So you USE JavaScript, and then you pipe your text through the js filter before it goes anywhere near document.write. Simple as that. Periodt. πŸŽ€


✨ DESCRIPTION ✨

Template::Plugin::JavaScript is a TT filter β€” a Template Toolkit filter, for the uninitiated β€” that filters your text so it can be safely used inside JavaScript quotes. Because raw text inside JS quotes? Absolutely not. It would simply end your whole script. 😭

So like, watch this:

  [% USE JavaScript %]
  document.write("[% FILTER js %]
  Here's some text going on.
  [% END %]");

will become:

  document.write("\nHere\'s some text going on.\n");

See what happened there? The newlines turned into \n, and that little apostrophe in "Here's" got itself an escape backslash and became \'. So now the quotes don't get confused, the string stays a string, and nobody's JavaScript breaks. It's honestly iconic behaviour. πŸ‘‘


πŸ’œ AUTHOR πŸ’œ

The original idea comes from Movable Type's encode_js global filter. Credit where credit is due, fr fr. 🫢

Tatsuhiko Miyagawa <miyagawa@bulknews.net>

And the licensing tea: this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Slay. 🌈


πŸ”₯ SEE ALSO πŸ”₯

  • Apache::JavaScript::DocumentWrite β€” another lil friend in this same general department, go check it out bestie. πŸ’–

and that's the whole page, babe πŸ’… β€” this was perl v5.36.0, dated 2022-12-10, straight from the Templa...Script(3pm) corner of the manual. go forth and escape your strings responsibly πŸ’–βœ¨

πŸ“¦ where this page comes from