blib/lib/Mojolicious/Plugin/InlineJSON.pm

filter: Statementstmt 84.0% 21 / 25 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 100% 4 / 4 totaltotal 87.5% 35 / 40 CC 1 SCAR 0.9
CC
1
Cov
84%
register
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Mojolicious::Plugin::InlineJSON;
2  
3 1 our $VERSION = '1.000_000'; # 1.0.0
4 1 $VERSION = eval $VERSION;
5  
6 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Mojo::ByteStream qw(b);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Mojo::JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::Util qw(xml_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub register {
12 0 ▶ my ($self, $app) = @_;
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
13 0 $app->helper(js_data => \&js_data);
14 0 $app->helper(js_json_string => \&js_json_string);
15 0 $app->helper(js_data_via_json => \&js_data_via_json);
16 }
17  
18 5 ▶sub _escape_tag { $_[0] =~ s/>/\\>/gr }
Subroutine _escape_tag: called CC=1 SCAR=0.0
19  
20 2 ▶sub _js_data { _escape_tag(encode_json($_[1])) }
Subroutine _js_data: called CC=1 SCAR=0.0
21  
22 3 ▶sub _js_json_string { _escape_tag(encode_json(encode_json($_[1]))) }
Subroutine _js_json_string: called CC=1 SCAR=0.0
23  
24 # returns '{ "foo": 1 }'
25  
26 2 ▶sub js_data { b(&_js_data) }
Subroutine js_data: called CC=1 SCAR=0.0
Pod documented
27  
28 # returns '"{ \"foo\": 1 }"'
29  
30 2 ▶sub js_json_string { b(&_js_json_string) }
Subroutine js_json_string: called CC=1 SCAR=0.0
Pod documented
31  
32 # returns 'JSON.parse("{ \"foo\": 1 }")'
33  
34 1 ▶sub js_data_via_json { b('JSON.parse('.&_js_json_string.')') }
Subroutine js_data_via_json: called CC=1 SCAR=0.0
Pod documented
35  
36 1 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 <script>
58 // bootstrap with literal JSON
59 var prerenderedData = <%= js_data($important_stuff) %>
60 </script>
61  
62  
63 =head1 DESCRIPTION
64  
65 L<Mojolicious::Plugin::InlineJSON> is a L<Mojolicious plugin|Mojolicious::Plugin>
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 <script>
78 var prerenderedData = <%= js_data($important_stuff) %>
79 // ...
80 </script>
81  
82 C<js_data> 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 <script>
100 var jsonString = <%= js_json_string($important_stuff) %>
101 var decoded = JSON.parse(jsonString)
102 // ...
103 <script>
104  
105 C<js_json_string> will turn the perl data structure into JSON, and
106 then turn that into a string which can be parsed with
107 C<JSON.parse()> in JS. This can be useful for places where your
108 code would've expected an XHR that you decode.
109  
110 =head2 js_data_via_json
111  
112 <script>
113 var decodedData = <%= js_data_via_json($important_stuff) %>
114 // ...
115 <script>
116  
117 C<js_data_via_json> is similar to C<js_json_string>, but it also
118 does the JSON.parse for you.
119  
120 =head1 AUTHORS
121  
122 =over 4
123  
124 =item mst - Matt S. Trout (cpan:MSTROUT) C<mst@shadowcat.co.uk>
125  
126 =item veesh - Veesh Goldman (cpan:VEESH) C<veesh@cpan.org>
127  
128 =back
129  
130 =head1 CONTRIBUTORS
131  
132 None yet - maybe this software is perfect! (ahahahahahahahahaha)
133  
134 =head1 COPYRIGHT
135  
136 Copyright (c) 2020 the Mojolicious::Plugin::InlineJSON L</AUTHORS> and L</CONTRIBUTORS>
137 as listed above.
138  
139 =head1 LICENSE
140  
141 This library is free software and may be distributed under the same terms
142 as perl itself.
143  
144 =cut
↑ Summary