From 71878bf7c1ffb5eeb04dd9f85bc883e9796cfa6a Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 27 Dec 2010 14:04:32 +0100 Subject: [PATCH] =?utf8?q?Helferfunktionen=20f=C3=BCr=20AJAX-Aufrufe/DOM-M?= =?utf8?q?odifikationen=20mit=20jQuery:=20Elemente=20ersetzen?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Template/Plugin/JavaScript.pm | 124 ++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 3 deletions(-) diff --git a/SL/Template/Plugin/JavaScript.pm b/SL/Template/Plugin/JavaScript.pm index 01e5104fa..241ea0ea3 100644 --- a/SL/Template/Plugin/JavaScript.pm +++ b/SL/Template/Plugin/JavaScript.pm @@ -6,21 +6,139 @@ use Template::Plugin; use strict; sub new { - my $class = shift; - my $context = shift; + my ($class, $context, @args) = @_; - bless { }, $class; + return bless { + CONTEXT => $context, + }, $class; } +# +# public interface +# + sub escape { my $self = shift; my $text = shift; + $text =~ s|\\|\\\\|g; $text =~ s|\"|\\\"|g; + $text =~ s|\n|\\n|g; return $text; } +sub replace_with { + return _replace_helper('replaceWith', @_); +} + +sub replace_html_with { + return _replace_helper('html', @_); +} + +# +# private methods +# + +sub _context { + die 'not an accessor' if @_ > 1; + return $_[0]->{CONTEXT}; +} + +sub _replace_helper { + my ($method, $self, $selector, $template, $locals) = @_; + + $template .= '.html' unless $template =~ m/\.html$/; + my $html = $self->escape($self->_context->process($template, %{ $locals || { } })); + my $code = < + +Returns C<$value> escaped for inclusion in a JavaScript string. The +value is not wrapped in quotes. Example: + + + +=item C + +Returns code replacing the DOM elements matched by C<$selector> with +the content rendered by Template's I directive applied to +C<$template>. C<%locals> are passed as local parameters to I. + +Uses jQuery's C function. Requires jQuery to be loaded. + +Example: + +
TODO:
+
    +
  • First item
  • +
  • Second item
  • +
  • Another item
  • +
+ + + + + +=item C + +Returns code replacing the inner HTML of the DOM elements matched by +C<$selector> with the content rendered by Template's I +directive applied to C<$template>. C<%locals> are passed as local +parameters to I. + +Uses jQuery's C function. Requires jQuery to be loaded. + +
TODO:
+
    +
  • First item
  • +
  • Second item
  • +
  • Another item
  • +
+ + + + + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut -- 2.20.1