1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
9 package SL::Template::Simple;
14 # 1. The template's file name
15 # 2. A reference to the Form object
16 # 3. A reference to the myconfig hash
19 # A new template object
33 $self->{source} = shift;
34 $self->{form} = shift;
35 $self->{myconfig} = shift;
36 $self->{userspath} = shift;
38 $self->{error} = undef;
39 $self->{quot_re} = '"';
41 $self->set_tag_style('<%', '%>');
46 my $tag_start = shift;
49 $self->{tag_start} = $tag_start;
50 $self->{tag_end} = $tag_end;
51 $self->{tag_start_qm} = quotemeta $tag_start;
52 $self->{tag_end_qm} = quotemeta $tag_end;
54 $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}";
62 # 1. A typeglob for the file handle. The output will be written
63 # to this file handle.
66 # 1 on success and undef or 0 if there was an error. In the latter case
67 # the calling function can retrieve the error message via $obj->get_error()
72 print(OUT "Hallo!\n");
78 return $self->{"error"};
85 sub _get_loop_variable {
88 my $get_array = shift;
91 my $form = $self->{form};
94 if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
95 $value = $form->{TEMPLATE_ARRAYS}->{$var};
97 $value = $form->{$var};
100 for (my $i = 0; $i < scalar(@indices); $i++) {
101 last unless (ref($value) eq "ARRAY");
102 $value = $value->[$indices[$i]];
108 sub substitute_vars {
109 my ($self, $text, @indices) = @_;
111 my $form = $self->{"form"};
113 while ($text =~ /$self->{substitute_vars_re}/) {
114 my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
115 my ($var, @options) = split(/\s+/, $1);
117 my $value = $self->_get_loop_variable($var, 0, @indices);
118 $value = $self->format_string($value) unless (grep(/^NOESCAPE$/, @options));
120 substr($text, $tag_pos, $tag_len, $value);
126 sub _parse_block_if {
127 $main::lxdebug->enter_sub();
130 my $contents = shift;
131 my $new_contents = shift;
135 $$new_contents .= $self->substitute_vars(substr($$contents, 0, $pos_if), @indices);
136 substr($$contents, 0, $pos_if) = "";
138 if ($$contents !~ m/^$self->{tag_start_qm}if
140 (not\b|\!)? # $1 -- Eventuelle Negierung
142 (\b.+?\b) # $2 -- Name der zu überprüfenden Variablen
143 ( # $3 -- Beginn des optionalen Vergleiches
145 ([!=]) # $4 -- Negierung des Vergleiches speichern
146 ([=~]) # $5 -- Art des Vergleiches speichern
148 ( # $6 -- Gequoteter String oder Bareword
150 (.*?)(?<!\\) # $7 -- Gequoteter String -- direkter Vergleich mit eq bzw. ne oder Patternmatching; Escapete Anführungs als Teil des Strings belassen
153 (\b.+?\b) # $8 -- Bareword -- als Index für $form benutzen
159 $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
160 $main::lxdebug->leave_sub();
166 my $comparison = $3; # Optionaler Match um $4..$8
167 my $operator_neg = $4; # '=' oder '!' oder undef, wenn kein Vergleich erkannt
168 my $operator_type = $5; # '=' oder '~' für Stringvergleich oder Regex
169 my $quoted_word = $7; # nur gültig, wenn quoted string angegeben (siehe unten); dann "value" aus <%if var == "value" %>
170 my $bareword = $8; # undef, falls quoted string angegeben wurde; andernfalls "othervar" aus <%if var == othervar %>
172 $not = !$not if ($operator_neg && $operator_neg eq '!');
174 substr($$contents, 0, length($&)) = "";
177 ($block, $$contents) = $self->find_end($$contents, 0, "$var $comparison", $not);
179 $self->{"error"} = "Unclosed $self->{tag_start}if$self->{tag_end}." unless ($self->{"error"});
180 $main::lxdebug->leave_sub();
184 my $value = $self->_get_loop_variable($var, 0, @indices);
187 if ($operator_type) {
188 my $compare_to = $bareword ? $self->_get_loop_variable($bareword, 0, @indices) : $quoted_word;
189 if ($operator_type eq '=') {
190 $hit = ($not && !($value eq $compare_to)) || (!$not && ($value eq $compare_to));
192 $hit = ($not && !($value =~ m/$compare_to/i)) || (!$not && ($value =~ m/$compare_to/i));
196 $hit = ($not && ! $value) || (!$not && $value);
200 my $new_text = $self->parse_block($block, @indices);
201 if (!defined($new_text)) {
202 $main::lxdebug->leave_sub();
205 $$new_contents .= $new_text;
208 $main::lxdebug->leave_sub();