1 package SL::Template::Plugin::JavaScript;
3 use base qw( Template::Plugin );
9 my ($class, $context, @args) = @_;
32 return _replace_helper('replaceWith', @_);
35 sub replace_html_with {
36 return _replace_helper('html', @_);
44 die 'not an accessor' if @_ > 1;
45 return $_[0]->{CONTEXT};
49 my ($method, $self, $selector, $template, $locals) = @_;
51 $template .= '.html' unless $template =~ m/\.html$/;
52 my $html = $self->escape($self->_context->process($template, %{ $locals || { } }));
54 \$('${selector}').${method}("$html");
71 SL::Template::Plugin::JavaScript - Template plugin for JavaScript helper functions
77 =item C<escape $value>
79 Returns C<$value> escaped for inclusion in a JavaScript string. The
80 value is not wrapped in quotes. Example:
82 <input type="submit" value="Delete"
83 onclick="if (confirm('Do you really want to delete this: [% JavaScript.escape(obj.description) %]') return true; else return false;">
85 =item C<replace_with $selector, $template, %locals>
87 Returns code replacing the DOM elements matched by C<$selector> with
88 the content rendered by Template's I<PROCESS> directive applied to
89 C<$template>. C<%locals> are passed as local parameters to I<PROCESS>.
91 Uses jQuery's C<obj.replaceWith()> function. Requires jQuery to be loaded.
97 <li id="item1">First item</li>
98 <li id="item2">Second item</li>
99 <li id="item3">Another item</li>
102 <script type="text/javascript">
104 [% JavaScript.replace_with('#item2', 'todo/single_item', item => current_todo_item) %]
108 <input type="submit" onclick="do_work(); return false;" value="Replace single item">
110 =item C<replace_html_with $selector, $template, %locals>
112 Returns code replacing the inner HTML of the DOM elements matched by
113 C<$selector> with the content rendered by Template's I<PROCESS>
114 directive applied to C<$template>. C<%locals> are passed as local
115 parameters to I<PROCESS>.
117 Uses jQuery's C<obj.html()> function. Requires jQuery to be loaded.
121 <li id="item1">First item</li>
122 <li id="item2">Second item</li>
123 <li id="item3">Another item</li>
126 <script type="text/javascript">
128 [% JavaScript.replace_html_with('#todo_list', 'todo/full_list', items => todo_items) %]
132 <input type="submit" onclick="do_work(); return false;" value="Replace list">
142 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>