Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 5a545036

Von Moritz Bunkus vor mehr als 15 Jahren hinzugefügt

  • ID 5a545036cdd7a0a4d9e65eb042db75c09a06aba5
  • Vorgänger 46a67f95
  • Nachfolger 40534aad

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.

Unterschiede anzeigen:

SL/Template.pm
39 39
}
40 40

  
41 41
sub set_tag_style {
42
  my $self              = shift;
43
  my $tag_start         = shift;
44
  my $tag_end           = shift;
45

  
46
  $self->{tag_start}    = $tag_start;
47
  $self->{tag_end}      = $tag_end;
48
  $self->{tag_start_qm} = quotemeta $tag_start;
49
  $self->{tag_end_qm}   = quotemeta $tag_end;
42
  my $self                    = shift;
43
  my $tag_start               = shift;
44
  my $tag_end                 = shift;
45

  
46
  $self->{tag_start}          = $tag_start;
47
  $self->{tag_end}            = $tag_end;
48
  $self->{tag_start_qm}       = quotemeta $tag_start;
49
  $self->{tag_end_qm}         = quotemeta $tag_end;
50

  
51
  $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}";
50 52
}
51 53

  
52 54
sub cleanup {
......
77 79
  return 0;
78 80
}
79 81

  
82
sub _get_loop_variable_value {
83
  my $self    = shift;
84
  my $var     = shift;
85
  my @indices = @_;
86

  
87
  my $form    = $self->{form};
88
  my $value   = $self->{form}->{$var};
89

  
90
  for (my $i = 0; $i < scalar(@indices); $i++) {
91
    last unless (ref($value) eq "ARRAY");
92
    $value = $value->[$indices[$i]];
93
  }
94

  
95
  return $value;
96
}
97

  
98
sub substitute_vars {
99
  my ($self, $text, @indices) = @_;
100

  
101
  my $form = $self->{"form"};
102

  
103
  while ($text =~ /$self->{substitute_vars_re}/) {
104
    my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
105
    my ($var, @options)     = split(/\s+/, $1);
106

  
107
    my $value               = $self->_get_loop_variable_value($var, @indices);
108
    $value                  = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
109

  
110
    substr($text, $tag_pos, $tag_len, $value);
111
  }
112

  
113
  return $text;
114
}
115

  
80 116
1;
81 117

  
82 118
####
......
92 128
sub new {
93 129
  my $type = shift;
94 130

  
95
  return $type->SUPER::new(@_);
131
  my $self = $type->SUPER::new(@_);
132

  
133
  return $self;
96 134
}
97 135

  
98 136
sub format_string {
......
117 155
  return $variable;
118 156
}
119 157

  
120
sub substitute_vars {
121
  my ($self, $text, @indices) = @_;
122

  
123
  my $form = $self->{"form"};
124

  
125
  while ($text =~ /$self->{tag_start_qm}(.+?)$self->{tag_end_qm}/) {
126
    my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
127
    my ($var, @options) = split(/\s+/, $1);
128
    my $value = $form->{$var};
129

  
130
    for (my $i = 0; $i < scalar(@indices); $i++) {
131
      last unless (ref($value) eq "ARRAY");
132
      $value = $value->[$indices[$i]];
133
    }
134
    $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
135
    substr($text, $tag_pos, $tag_len) = $value;
136
  }
137

  
138
  return $text;
139
}
140

  
141 158
sub parse_foreach {
142 159
  my ($self, $var, $text, $start_tag, $end_tag, @indices) = @_;
143 160

  
......
756 773
    }
757 774
  }
758 775

  
759
  $self->{"rnd"} = int(rand(1000000));
776
  $self->{"rnd"}   = int(rand(1000000));
760 777
  $self->{"iconv"} = Text::Iconv->new($main::dbcharset, "UTF-8");
761 778

  
762
  return $self;
763
}
764

  
765
sub substitute_vars {
766
  my ($self, $text, @indices) = @_;
767

  
768
  my $form = $self->{"form"};
779
  $self->set_tag_style('&lt;%', '%&gt;');
769 780

  
770
  while ($text =~ /\&lt;\%(.*?)\%\&gt;/) {
771
    my $value = $form->{$1};
772

  
773
    for (my $i = 0; $i < scalar(@indices); $i++) {
774
      last unless (ref($value) eq "ARRAY");
775
      $value = $value->[$indices[$i]];
776
    }
777
    substr($text, $-[0], $+[0] - $-[0]) = $self->format_string($value);
778
  }
779

  
780
  return $text;
781
  return $self;
781 782
}
782 783

  
783 784
sub parse_foreach {

Auch abrufbar als: Unified diff