logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

Template::Plugin::JSON::Escape - Adds a .json vmethod and a json filter.

Author

       nanto_vi (TOYAMA Nao) <nanto@moon.email.ne.jp>

Description

       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.

       It also provides decoding function to keep compatibility with Template::Plugin::JSON.

Features

USEJSON.Escape
       Any options on the USE line are passed through to the JSON object, much like "to_json" in JSON.

   jsonvmethod
       A ".json" vmethod converts scalars, arrays and hashes into corresponding JSON strings.

           [% json_stuct = { foo => 42, bar => [ 1, 2, 3 ] } %]

           <script>
               var json = [% json_struct.json %];
           </script>

           <span onclick="doSomething([% json_struct.json %]);">

   jsonfilter
       A "json" filter escapes "<", ">", "&", "+", "U+2028" and "U+2029" as "\uxxxx". In the attribute, you may
       just use an "html" filter.

           [% json_string = '{ "foo": 42, "bar": [ 1, 2, 3 ] }' %]

           <script>
               var json = [% json_string | json %];
           </script>

           <span onclick="doSomething([% json_string | html %]);">

   json_decodemethod
       A "json_decode" method allow you to convert from a JSON string into a corresponding data structure.

           [% SET json_struct = JSON.Escape.json_decode(json_string) %]
           [% json_struct.foo | html %]

Name

       Template::Plugin::JSON::Escape - Adds a .json vmethod and a json filter.

See Also

       Template::Plugin::JSON, JSON, Template::Plugin

Synopsis

           [% 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 %]

Version Control

       <https://github.com/nanto/perl-Template-Plugin-JSON-Escape>

See Also