From 5a545036cdd7a0a4d9e65eb042db75c09a06aba5 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Thu, 7 May 2009 14:41:59 +0000 Subject: [PATCH] =?utf8?q?Den=20Code=20f=C3=BCr=20das=20Ersetzen=20von=20V?= =?utf8?q?ariablen=20in=20die=20gemeinsame=20Basisklasse=20ausgelagert=20-?= =?utf8?q?-=20er=20unterscheidet=20sich=20f=C3=BCr=20die=20einzelnen=20Vor?= =?utf8?q?lagentypen=20nur=20im=20regul=C3=A4ren=20Ausdruck=20zur=20Erkenn?= =?utf8?q?ung=20der=20Variablen.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Template.pm | 99 +++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/SL/Template.pm b/SL/Template.pm index 8a9e68326..c273d8f2d 100644 --- a/SL/Template.pm +++ b/SL/Template.pm @@ -39,14 +39,16 @@ sub _init { } sub set_tag_style { - my $self = shift; - my $tag_start = shift; - my $tag_end = shift; - - $self->{tag_start} = $tag_start; - $self->{tag_end} = $tag_end; - $self->{tag_start_qm} = quotemeta $tag_start; - $self->{tag_end_qm} = quotemeta $tag_end; + my $self = shift; + my $tag_start = shift; + my $tag_end = shift; + + $self->{tag_start} = $tag_start; + $self->{tag_end} = $tag_end; + $self->{tag_start_qm} = quotemeta $tag_start; + $self->{tag_end_qm} = quotemeta $tag_end; + + $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}"; } sub cleanup { @@ -77,6 +79,40 @@ sub uses_temp_file { return 0; } +sub _get_loop_variable_value { + my $self = shift; + my $var = shift; + my @indices = @_; + + my $form = $self->{form}; + my $value = $self->{form}->{$var}; + + for (my $i = 0; $i < scalar(@indices); $i++) { + last unless (ref($value) eq "ARRAY"); + $value = $value->[$indices[$i]]; + } + + return $value; +} + +sub substitute_vars { + my ($self, $text, @indices) = @_; + + my $form = $self->{"form"}; + + while ($text =~ /$self->{substitute_vars_re}/) { + my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]); + my ($var, @options) = split(/\s+/, $1); + + my $value = $self->_get_loop_variable_value($var, @indices); + $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options)); + + substr($text, $tag_pos, $tag_len, $value); + } + + return $text; +} + 1; #### @@ -92,7 +128,9 @@ use vars qw(@ISA); sub new { my $type = shift; - return $type->SUPER::new(@_); + my $self = $type->SUPER::new(@_); + + return $self; } sub format_string { @@ -117,27 +155,6 @@ sub format_string { return $variable; } -sub substitute_vars { - my ($self, $text, @indices) = @_; - - my $form = $self->{"form"}; - - while ($text =~ /$self->{tag_start_qm}(.+?)$self->{tag_end_qm}/) { - my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]); - my ($var, @options) = split(/\s+/, $1); - my $value = $form->{$var}; - - for (my $i = 0; $i < scalar(@indices); $i++) { - last unless (ref($value) eq "ARRAY"); - $value = $value->[$indices[$i]]; - } - $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options)); - substr($text, $tag_pos, $tag_len) = $value; - } - - return $text; -} - sub parse_foreach { my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_; @@ -756,28 +773,12 @@ sub new { } } - $self->{"rnd"} = int(rand(1000000)); + $self->{"rnd"} = int(rand(1000000)); $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8"); - return $self; -} - -sub substitute_vars { - my ($self, $text, @indices) = @_; - - my $form = $self->{"form"}; + $self->set_tag_style('<%', '%>'); - while ($text =~ /\<\%(.*?)\%\>/) { - my $value = $form->{$1}; - - for (my $i = 0; $i < scalar(@indices); $i++) { - last unless (ref($value) eq "ARRAY"); - $value = $value->[$indices[$i]]; - } - substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value); - } - - return $text; + return $self; } sub parse_foreach { -- 2.20.1