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 \s-1JSON\s0 strings in \s-1HTML\s0. In the output, special characters such as \*(C`<\*(C' and \*(C`&\*(C' are escaped as \*(C`\uxxxx\*(C' to prevent \s-1XSS\s0 attacks.
It also provides decoding function to keep compatibility with Template::Plugin::JSON.
Any options on the \s-1USE\s0 line are passed through to the \s-1JSON\s0 object, much like \*(L"to_json\*(R" in \s-1JSON\s0.
A \*(C`.json\*(C' vmethod converts scalars, arrays and hashes into corresponding \s-1JSON\s0 strings.
[% json_stuct = { foo => 42, bar => [ 1, 2, 3 ] } %]
<script> var json = [% json_struct.json %]; </script>
<span onclick="doSomething([% json_struct.json %]);">
A \*(C`json\*(C' filter escapes \*(C`<\*(C', \*(C`>\*(C', \*(C`&\*(C', \*(C`+\*(C', \*(C`U+2028\*(C' and \*(C`U+2029\*(C' as \*(C`\uxxxx\*(C'. In the attribute, you may just use an \*(C`html\*(C' filter.
[% json_string = '{ "foo": 42, "bar": [ 1, 2, 3 ] }' %]
<script> var json = [% json_string | json %]; </script>
<span onclick="doSomething([% json_string | html %]);">
A \*(C`json_decode\*(C' method allow you to convert from a \s-1JSON\s0 string into a corresponding data structure.
[% SET json_struct = JSON.Escape.json_decode(json_string) %] [% json_struct.foo | html %]
Template::Plugin::JSON, \s-1JSON\s0, Template::Plugin
https://github.com/nanto/perl-Template-Plugin-JSON-Escape <https://github.com/nanto/perl-Template-Plugin-JSON-Escape>
nanto_vi (\s-1TOYAMA\s0 Nao) <[email protected]>
Copyright (c) 2011 nanto_vi (\s-1TOYAMA\s0 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 \*(L"Software\*(R"), 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.
\s-1THE\s0 \s-1SOFTWARE\s0 \s-1IS\s0 \s-1PROVIDED\s0 \*(L"\s-1AS\s0 \s-1IS\s0\*(R", \s-1WITHOUT\s0 \s-1WARRANTY\s0 \s-1OF\s0 \s-1ANY\s0 \s-1KIND\s0, \s-1EXPRESS\s0 \s-1OR\s0 \s-1IMPLIED\s0, \s-1INCLUDING\s0 \s-1BUT\s0 \s-1NOT\s0 \s-1LIMITED\s0 \s-1TO\s0 \s-1THE\s0 \s-1WARRANTIES\s0 \s-1OF\s0 \s-1MERCHANTABILITY\s0, \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0 \s-1AND\s0 \s-1NONINFRINGEMENT\s0. \s-1IN\s0 \s-1NO\s0 \s-1EVENT\s0 \s-1SHALL\s0 \s-1THE\s0 \s-1AUTHORS\s0 \s-1OR\s0 \s-1COPYRIGHT\s0 \s-1HOLDERS\s0 \s-1BE\s0 \s-1LIABLE\s0 \s-1FOR\s0 \s-1ANY\s0 \s-1CLAIM\s0, \s-1DAMAGES\s0 \s-1OR\s0 \s-1OTHER\s0 \s-1LIABILITY\s0, \s-1WHETHER\s0 \s-1IN\s0 \s-1AN\s0 \s-1ACTION\s0 \s-1OF\s0 \s-1CONTRACT\s0, \s-1TORT\s0 \s-1OR\s0 \s-1OTHERWISE\s0, \s-1ARISING\s0 \s-1FROM\s0, \s-1OUT\s0 \s-1OF\s0 \s-1OR\s0 \s-1IN\s0 \s-1CONNECTION\s0 \s-1WITH\s0 \s-1THE\s0 \s-1SOFTWARE\s0 \s-1OR\s0 \s-1THE\s0 \s-1USE\s0 \s-1OR\s0 \s-1OTHER\s0 \s-1DEALINGS\s0 \s-1IN\s0 \s-1THE\s0 \s-1SOFTWARE\s0.