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