line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::InlineJSON; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.009_001'; # 0.9.1 |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
69724
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
195815
|
|
|
1
|
|
|
|
|
9
|
|
6
|
1
|
|
|
1
|
|
1813
|
use Mojo::ByteStream qw(b); |
|
1
|
|
|
|
|
4080
|
|
|
1
|
|
|
|
|
61
|
|
7
|
1
|
|
|
1
|
|
548
|
use Mojo::JSON qw(encode_json); |
|
1
|
|
|
|
|
22971
|
|
|
1
|
|
|
|
|
93
|
|
8
|
1
|
|
|
1
|
|
10
|
use Mojo::Util qw(xml_escape); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
361
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub register { |
11
|
0
|
|
|
0
|
1
|
0
|
my ($self, $app) = @_; |
12
|
0
|
|
|
|
|
0
|
$app->helper(js_data => \&js_data); |
13
|
0
|
|
|
|
|
0
|
$app->helper(js_json_string => \&js_json_string); |
14
|
0
|
|
|
|
|
0
|
$app->helper(js_data_via_json => \&js_data_via_json); |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
5
|
|
|
5
|
|
555
|
sub _escape_tag { $_[0] =~ s/>/\\>/gr } |
18
|
|
|
|
|
|
|
|
19
|
2
|
|
|
2
|
|
17
|
sub _js_data { _escape_tag(encode_json($_[1])) } |
20
|
|
|
|
|
|
|
|
21
|
3
|
|
|
3
|
|
12
|
sub _js_json_string { _escape_tag(encode_json(encode_json($_[1]))) } |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# returns '{ "foo": 1 }' |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
2
|
1
|
4019
|
sub js_data { b(&_js_data) } |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# returns '"{ \"foo\": 1 }"' |
28
|
|
|
|
|
|
|
|
29
|
2
|
|
|
2
|
1
|
1459
|
sub js_json_string { b(&_js_json_string) } |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# returns 'JSON.parse("{ \"foo\": 1 }")' |
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
1
|
1
|
637
|
sub js_data_via_json { b('JSON.parse('.&_js_json_string.')') } |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
9201; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=encoding utf8 |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=head1 NAME |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Mojolicious::Plugin::InlineJSON - Bootstrap your app with inline JSON |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=head1 SYNOPSIS |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# Mojolicious |
46
|
|
|
|
|
|
|
use Mojolicious; |
47
|
|
|
|
|
|
|
$app->plugin('InlineJSON'); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Mojolicious::Lite |
50
|
|
|
|
|
|
|
plugin 'InlineJSON'; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# in your controller |
53
|
|
|
|
|
|
|
$c->stash(important_stuff => { data => [ ... ] }); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# then, in a template |
56
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 DESCRIPTION |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
L is a L |
65
|
|
|
|
|
|
|
for rendering data to json in a template. This is useful for when |
66
|
|
|
|
|
|
|
you want to serve content managed dynamically by javascript |
67
|
|
|
|
|
|
|
without needing any extra AJAX calls after the page loads. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
This plugin provides 3 different helpers for rendering JSON in a |
70
|
|
|
|
|
|
|
variety of different ways. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 HELPERS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=head2 js_data |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
C will render the perl data structure passed to it into a |
82
|
|
|
|
|
|
|
literal javascript structure, capable of being directly consumed |
83
|
|
|
|
|
|
|
by javascript. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
In essence, it turns this |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
{ key => 'value' } |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
into |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
{ key: 'value' } |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
while making sure to avoid any attribute escaping or accidental |
94
|
|
|
|
|
|
|
tag closure. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head2 js_json_string |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|