Template::Plugin::JSON::Escape(3pm)

Template::Plugin::JSON::Escape3pm

Adds a .json vmethod and a json filter.

Usage

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

Overview

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

USE JSON.Escape

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

json vmethod

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 %]);">

json filter

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_decode method

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

Version control

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

Releases

7 releases since Debian 7 (Wheezy); the text changed 1 time.

  1. 7–10
  2. 11–13

Author

nanto_vi (TOYAMA Nao) <[email protected]>

From libtemplate-plugin-json-escape-perl 0.2-1.1 in Debian 13 (Trixie). Parsed by carta from the page's man source.