1 package SL::Template::Plugin::JavaScript;
3 use base qw( Template::Plugin::Filter );
12 return $cached_instance ||= $class->SUPER::new(@_);
18 $self->install_filter($self->{ _ARGS }->[0] || 'js');
27 # see ecmascript spec section 7.8.4
28 my @escape_chars = ('\\', '\'', '"');
35 "\x0B" => 'v', # noone uses vertical tab anyway...
37 my $re = join '', map { qr/($_)/s } join '|', keys(%control_chars), map { "\Q$_\E" } @escape_chars;
43 $text =~ s/$re/'\\' . ($control_chars{$1} || $1)/egs;
49 my ($self, $text) = @_;
50 return $self->escape($text);
54 return _replace_helper('replaceWith', @_);
57 sub replace_html_with {
58 return _replace_helper('html', @_);
66 die 'not an accessor' if @_ > 1;
67 return $_[0]->{CONTEXT};
71 my ($method, $self, $selector, $template, $locals) = @_;
73 $template .= '.html' unless $template =~ m/\.html$/;
74 my $html = $self->escape($self->_context->process($template, %{ $locals || { } }));
76 \$('${selector}').${method}("$html");
93 SL::Template::Plugin::JavaScript - Template plugin for JavaScript helper functions
99 =item C<escape $value>
101 Returns C<$value> escaped for inclusion in a JavaScript string. The
102 value is not wrapped in quotes. Example:
104 <input type="submit" value="Delete"
105 onclick="if (confirm('Do you really want to delete this: [% JavaScript.escape(obj.description) %]') return true; else return false;">
107 You can also use the filter syntax instead:
109 <input type="submit" value="Delete"
110 onclick="if (confirm('Do you really want to delete this: [% obj.description | js %]') return true; else return false;">
112 =item C<replace_with $selector, $template, %locals>
114 Returns code replacing the DOM elements matched by C<$selector> with
115 the content rendered by Template's I<PROCESS> directive applied to
116 C<$template>. C<%locals> are passed as local parameters to I<PROCESS>.
118 Uses jQuery's C<obj.replaceWith()> function. Requires jQuery to be loaded.
124 <li id="item1">First item</li>
125 <li id="item2">Second item</li>
126 <li id="item3">Another item</li>
129 <script type="text/javascript">
131 [% JavaScript.replace_with('#item2', 'todo/single_item', item => current_todo_item) %]
135 <input type="submit" onclick="do_work(); return false;" value="Replace single item">
137 =item C<replace_html_with $selector, $template, %locals>
139 Returns code replacing the inner HTML of the DOM elements matched by
140 C<$selector> with the content rendered by Template's I<PROCESS>
141 directive applied to C<$template>. C<%locals> are passed as local
142 parameters to I<PROCESS>.
144 Uses jQuery's C<obj.html()> function. Requires jQuery to be loaded.
148 <li id="item1">First item</li>
149 <li id="item2">Second item</li>
150 <li id="item3">Another item</li>
153 <script type="text/javascript">
155 [% JavaScript.replace_html_with('#todo_list', 'todo/full_list', items => todo_items) %]
159 <input type="submit" onclick="do_work(); return false;" value="Replace list">
169 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>