Template::Plugin::JSON::Escape - Adds a .json vmethod and a json filter.
[% USE JSON.Escape( pretty => 1 ) %]; <script type="text/javascript"> var foo = [% foo.json %]; var bar = [% json_string | json %] </script> or read in JSON [% USE JSON.Escape %] [% data = JSON.Escape.json_decode(json) %] [% data.thing %]
This plugin allows you to embed JSON strings in HTML. In the output, special characters such as < and & are escaped as \uxxxx to prevent XSS attacks.
<
&
\uxxxx
It also provides decoding function to keep compatibility with Template::Plugin::JSON.
Any options on the USE line are passed through to the JSON object, much like "to_json" in JSON.
A .json vmethod converts scalars, arrays and hashes into corresponding JSON strings.
.json
[% json_stuct = { foo => 42, bar => [ 1, 2, 3 ] } %] <script> var json = [% json_struct.json %]; </script> <span onclick="doSomething([% json_struct.json %]);">
A json filter escapes <, >, &, +, U+2028 and U+2029 as \uxxxx. In the attribute, you may just use an html filter.
json
>
+
U+2028
U+2029
html
[% json_string = '{ "foo": 42, "bar": [ 1, 2, 3 ] }' %] <script> var json = [% json_string | json %]; </script> <span onclick="doSomething([% json_string | html %]);">
A json_decode method allow you to convert from a JSON string into a corresponding data structure.
json_decode
[% SET json_struct = JSON.Escape.json_decode(json_string) %] [% json_struct.foo | html %]
Template::Plugin::JSON, JSON, Template::Plugin
https://github.com/nanto/perl-Template-Plugin-JSON-Escape
nanto_vi (TOYAMA Nao) <nanto@moon.email.ne.jp>
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.
To install Template::Plugin::JSON::Escape, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Template::Plugin::JSON::Escape
CPAN shell
perl -MCPAN -e shell install Template::Plugin::JSON::Escape
For more information on module installation, please visit the detailed CPAN module installation guide.