| 1 | package Template::Flute::Filter::JsonVar; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Template::Flute::Filter'; | ||
| 7 | use JSON; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Template::Flute::Filter::JsonVar - JSON to Javascript variable filter | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | Takes a Perl structure or a JSON string and returns Javascript | ||
| 16 | which parses the JSON string with Jquery and assigns it to | ||
| 17 | the variable C<json>. | ||
| 18 | |||
| 19 | The following example shows you how to inject the JSON variable | ||
| 20 | into the template: | ||
| 21 | |||
| 22 | HTML Template: | ||
| 23 | |||
| 24 | <script type="text/javascript" id="vars"></script> | ||
| 25 | |||
| 26 | XML Specification: | ||
| 27 | |||
| 28 | <value name="jsvars" id="vars" filter="json_var"/> | ||
| 29 | |||
| 30 | Values for L<Template::Flute>: | ||
| 31 | |||
| 32 | Template::Flute->new(..., | ||
| 33 | values => {jsvars => | ||
| 34 | {username => 'shopper@nitesi.biz', city => 'Vienna'}, | ||
| 35 | }, | ||
| 36 | ); | ||
| 37 | |||
| 38 | This results in the following JavaScript: | ||
| 39 | |||
| 40 | <script type="text/javascript" id="vars"> | ||
| 41 | var json = $.parseJSON('{"city":"Vienna","username":"shopper@nitesi.biz"}'); | ||
| 42 | </script> | ||
| 43 | |||
| 44 | =head1 PREREQUISITES | ||
| 45 | |||
| 46 | L<JSON> module. | ||
| 47 | |||
| 48 | =head1 METHODS | ||
| 49 | |||
| 50 | =head2 init | ||
| 51 | |||
| 52 | The init method allows you to set the following options: | ||
| 53 | |||
| 54 | =over 4 | ||
| 55 | |||
| 56 | =item engine | ||
| 57 | |||
| 58 | Value is either C<jquery> or C<eval>. | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | sub init { | ||
| 65 | my ($self, %args) = @_; | ||
| 66 | |||
| 67 | $self->{engine} = $args{options}->{engine}; | ||
| 68 | } | ||
| 69 | |||
| 70 | =head2 filter | ||
| 71 | |||
| 72 | Filters the given Perl structure to a JSON string. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub filter { | ||
| 77 | my ($self, $struct) = @_; | ||
| 78 | my ($json); | ||
| 79 | |||
| 80 | if (ref($struct)) { | ||
| 81 | # turn into JSON first | ||
| 82 | $json = to_json($struct, {pretty => 0}); | ||
| 83 | } | ||
| 84 | else { | ||
| 85 | $json = $struct; | ||
| 86 | } | ||
| 87 | |||
| 88 | unless ($self->{engine}) { | ||
| 89 | die "No engine specified for json_var filter."; | ||
| 90 | } | ||
| 91 | |||
| 92 | if ($self->{engine} eq 'jquery') { | ||
| 93 | return q{ | ||
| 94 | var json = $.parseJSON('} | ||
| 95 | . _json_filter($json) . q{');}; | ||
| 96 | } | ||
| 97 | |||
| 98 | if ($self->{engine} eq 'eval') { | ||
| 99 | return q{ | ||
| 100 | var json = eval('(} | ||
| 101 | . _json_filter($json) . q{)');}; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _json_filter { | ||
| 106 | my $value = shift; | ||
| 107 | |||
| 108 | return '' unless defined $value; | ||
| 109 | |||
| 110 | # escape single quote | ||
| 111 | $value =~ s!'!\\'!g; | ||
| 112 | |||
| 113 | # other escapes - from Template::Plugin::JSON::Escape | ||
| 114 | $value =~ s!&!\\u0026!g; | ||
| 115 | $value =~ s!<!\\u003c!g; | ||
| 116 | $value =~ s!>!\\u003e!g; | ||
| 117 | $value =~ s!\+!\\u002b!g; | ||
| 118 | $value =~ s!\x{2028}!\\u2028!g; | ||
| 119 | $value =~ s!\x{2029}!\\u2029!g; | ||
| 120 | $value; | ||
| 121 | } | ||
| 122 | |||
| 123 | 1; |