From: Moritz Bunkus Date: Thu, 7 May 2009 14:41:59 +0000 (+0000) Subject: Den Code für das Ersetzen von Variablen in die gemeinsame Basisklasse ausgelagert... X-Git-Tag: release-2.6.0beta2~3 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=5a545036cdd7a0a4d9e65eb042db75c09a06aba5;p=kivitendo-erp.git Den Code für das Ersetzen von Variablen in die gemeinsame Basisklasse ausgelagert -- er unterscheidet sich für die einzelnen Vorlagentypen nur im regulären Ausdruck zur Erkennung der Variablen. --- 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 {