1 package SL::Template::Plugin::JavaScript;
3 use base qw( Template::Plugin );
9 my ($class, $context, @args) = @_;
33 return _replace_helper('replaceWith', @_);
36 sub replace_html_with {
37 return _replace_helper('html', @_);
45 die 'not an accessor' if @_ > 1;
46 return $_[0]->{CONTEXT};
50 my ($method, $self, $selector, $template, $locals) = @_;
52 $template .= '.html' unless $template =~ m/\.html$/;
53 my $html = $self->escape($self->_context->process($template, %{ $locals || { } }));
55 \$('${selector}').${method}("$html");
72 SL::Template::Plugin::JavaScript - Template plugin for JavaScript helper functions
78 =item C<escape $value>
80 Returns C<$value> escaped for inclusion in a JavaScript string. The
81 value is not wrapped in quotes. Example:
83 <input type="submit" value="Delete"
84 onclick="if (confirm('Do you really want to delete this: [% JavaScript.escape(obj.description) %]') return true; else return false;">
86 =item C<replace_with $selector, $template, %locals>
88 Returns code replacing the DOM elements matched by C<$selector> with
89 the content rendered by Template's I<PROCESS> directive applied to
90 C<$template>. C<%locals> are passed as local parameters to I<PROCESS>.
92 Uses jQuery's C<obj.replaceWith()> function. Requires jQuery to be loaded.
98 <li id="item1">First item</li>
99 <li id="item2">Second item</li>
100 <li id="item3">Another item</li>
103 <script type="text/javascript">
105 [% JavaScript.replace_with('#item2', 'todo/single_item', item => current_todo_item) %]
109 <input type="submit" onclick="do_work(); return false;" value="Replace single item">
111 =item C<replace_html_with $selector, $template, %locals>
113 Returns code replacing the inner HTML of the DOM elements matched by
114 C<$selector> with the content rendered by Template's I<PROCESS>
115 directive applied to C<$template>. C<%locals> are passed as local
116 parameters to I<PROCESS>.
118 Uses jQuery's C<obj.html()> function. Requires jQuery to be loaded.
122 <li id="item1">First item</li>
123 <li id="item2">Second item</li>
124 <li id="item3">Another item</li>
127 <script type="text/javascript">
129 [% JavaScript.replace_html_with('#todo_list', 'todo/full_list', items => todo_items) %]
133 <input type="submit" onclick="do_work(); return false;" value="Replace list">
143 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>