Dotiac::DTL::Addon::json: Filters to generate JSON data
Load from a Dotiac::DTL-template:
{% load json %}
Load in Perl file for all templates:
use Dotiac::DTL::Addon::json;
Then it can be used:
{{ data|json|safe }} {{ data|json_ascii }} {{ data|json_pretty }} {{ data|json_pretty_ascii|safe }}
via CPAN:
perl -MCPAN -e "install Dotiac::DTL::Addon::json"
or get it from https://sourceforge.net/project/showfiles.php?group_id=249411&package_id=306751, extract it and then run in the extracted folder:
perl Makefile.PL make test make install
This provides some filters to output any type of data
Like most other filters, these will return a safe value on safe input. But string literals are always safe and will produce bad output, so beware of those.
{{ "Foo"|json }} {# "Foo" #} {{ "Foo"|json|escape }} {# "Foo" #}
Converts any value into JSON, even lists and dictionaries.
Output will be in UTF-8.
data=>{List=>[1,2,3],Value=>"Foo\x{34fc}"}; <a onclick="return {{ data|json }}"> {# <a onclick="return {"List":[1,2,3],"Value":"FooÒô+"}"> #} var Value={{ data|json|safe }} {# var Value={"List":[1,2,3],"Value":"FooÒô+"} #}
Like json, but the output will be in ascii. This is useful if the generated HTML page is not utf8.
data=>{List=>[1,2,3],Value=>"Foo"}; <a onclick="return {{ data|json_ascii }}"> {# <a onclick="return {"List":[1,2,3],"Value":"Foo\u34fc"}"> #} var Value={{ data|json_ascii|safe }} {# var Value={"List":[1,2,3],"Value":"Foo\u34fc"} #}
Like json, but with pretty output. This is much larger and mostly not needed.
data=>{List=>[1,2,3],Value=>"Foo"}; <a onclick="return {{ data|json_pretty }}"> {# <a onclick="return { "List" : [ 1, 2, 3 ], "Value" : "FooÒô+" }"> #} var Value={{ data|json_pretty|safe }} {# var Value={ "List" : [ 1, 2, 3 ], "Value" : "FooÒô+" } #}
Like json_ascii, but also with pretty output. This is much larger and mostly not needed.
data=>{List=>[1,2,3],Value=>"Foo"}; <a onclick="return {{ data|json_ascii_pretty }}"> {# <a onclick="return { "List" : [ 1, 2, 3 ], "Value" : "Foo\u34fc" }"> #} var Value={{ data|json_ascii_pretty|safe }} {# var Value={ "List" : [ 1, 2, 3 ], "Value" : "Foo\u34fc" } #}
Please report any bugs or feature requests to https://sourceforge.net/tracker2/?group_id=249411&atid=1126445
Dotiac::DTL, Dotiac::DTL::Addon, http://www.dotiac.com, http://www.djangoproject.com
Marc-Sebastian Lucksch
perl@marc-s.de
1 POD Error
The following errors were encountered while parsing the POD:
Non-ASCII character seen before =encoding in '{"List":[1,2,3],"Value":"FooÒô+"}">'. Assuming CP1252
To install Dotiac::DTL::Addon::json, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dotiac::DTL::Addon::json
CPAN shell
perl -MCPAN -e shell install Dotiac::DTL::Addon::json
For more information on module installation, please visit the detailed CPAN module installation guide.