Templa...Escape(3pm)
β¨ Template::Plugin::JSON::Escape β¨
okay bestie, so this page is the manual for Template::Plugin::JSON::Escape β a Perl thingy that lives in the User Contributed Perl Documentation corner of the world, and it is exactly as iconic as it sounds π
π NAME π
Template::Plugin::JSON::Escape β it adds a .json vmethod and a json filter. that's the whole vibe. no cap. π¦
π SYNOPSIS π
[% USE JSON.Escape( pretty => 1 ) %];
<script type="text/javascript">
var foo = [% foo.json %];
var bar = [% json_string | json %]
</script>
or, plot twist, you can read JSON in the other direction too π«Ά
[% USE JSON.Escape %]
[% data = JSON.Escape.json_decode(json) %]
[% data.thing %]
π DESCRIPTION π
so basically this plugin lets you embed JSON strings in HTML, and the way it does it is so careful of you. in the output, special characters like "<" and "&" get escaped as "\uxxxx" so nobody can pull an XSS attack on your ass. it's giving protective bestie energy π‘οΈβ¨
it also hands you a decoding function, so it stays compatible with Template::Plugin::JSON. teamwork, honestly. π
π FEATURES π
π§ USE JSON.Escape
any options you put on the USE line get passed straight through to the JSON object, much like "to_json" in JSON. so whatever you'd normally tell JSON, JSON hears it. periodt. π«
πΈ json vmethod
a ".json" vmethod converts scalars, arrays and hashes into their corresponding JSON strings. literally magic. π₯
[% json_stuct = { foo => 42, bar => [ 1, 2, 3 ] } %]
<script>
var json = [% json_struct.json %];
</script>
<span onclick="doSomething([% json_struct.json %]);">
(yes bestie, json_stuct is spelled like that in the original and i am not touching it β we preserve, we don't edit π)
π json filter
a "json" filter escapes "<", ">", "&", "+", "U+2028" and "U+2029" as "\uxxxx". and listen β in the attribute, you may just use an "html" filter instead. your call, main character. π
[% json_string = '{ "foo": 42, "bar": [ 1, 2, 3 ] }' %]
<script>
var json = [% json_string | json %];
</script>
<span onclick="doSomething([% json_string | html %]);">
π json_decode method
a "json_decode" method allow you to convert from a JSON string into a corresponding data structure. (that grammar is the original's, not mine, don't @ me π)
[% SET json_struct = JSON.Escape.json_decode(json_string) %]
[% json_struct.foo | html %]
π SEE ALSO π
go read these next, they're the friend group:
Template::Plugin::JSONJSONTemplate::Plugin
π¦ VERSION CONTROL π¦
the source lives here, go be nosy:
<https://github.com/nanto/perl-Template-Plugin-JSON-Escape>
β AUTHOR β
nanto_vi (TOYAMA Nao) <nanto@moon.email.ne.jp>
π COPYRIGHT & LICENSE π
Copyright (c) 2011 nanto_vi (TOYAMA Nao).
Copyright (c) 2006, 2008 Infinity Interactive, Yuval Kogman.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
okay that all-caps bit is scary but it's just legal boilerplate bestie, breathe π
and that's the whole page!! π this was perl v5.32.0, dated 2021-01-03, and you just read the whole Templa...Escape(3pm) manual like the icon you are. slay, go write some escaped JSON πβ¨π«Ά
π¦ where this page comes from
- debian bookworm: libtemplate-plugin-json-escape-perl 0.2-1.1 Β· MIT
- debian trixie: libtemplate-plugin-json-escape-perl 0.2-1.1 Β· MIT
- ubuntu jammy: libtemplate-plugin-json-escape-perl 0.2-1.1 Β· MIT
- ubuntu noble: libtemplate-plugin-json-escape-perl 0.2-1.1 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms