1 #====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #====================================================================
9 package SL::Template::Simple;
13 use Scalar::Util qw(blessed);
16 # 1. The template's file name
17 # 2. A reference to the Form object
18 # 3. A reference to the myconfig hash
21 # A new template object
35 $self->{source} = shift;
36 $self->{form} = shift;
37 $self->{myconfig} = shift;
38 $self->{userspath} = shift;
40 $self->{error} = undef;
41 $self->{quot_re} = '"';
43 $self->set_tag_style('<%', '%>');
48 my $tag_start = shift;
51 $self->{tag_start} = $tag_start;
52 $self->{tag_end} = $tag_end;
53 $self->{tag_start_qm} = quotemeta $tag_start;
54 $self->{tag_end_qm} = quotemeta $tag_end;
56 $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}";
64 # 1. A typeglob for the file handle. The output will be written
65 # to this file handle.
68 # 1 on success and undef or 0 if there was an error. In the latter case
69 # the calling function can retrieve the error message via $obj->get_error()
74 print(OUT "Hallo!\n");
80 return $self->{"error"};
87 sub _get_loop_variable {
88 my ($self, $var, $get_array, @indices) = @_;
89 my $form = $self->{form};
90 my ($value, @methods);
93 ($var, @methods) = split m/\./, $var;
96 if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
97 $value = $form->{TEMPLATE_ARRAYS}->{$var};
99 $value = $form->{$var};
102 for (my $i = 0; $i < scalar(@indices); $i++) {
103 last unless (ref($value) eq "ARRAY");
104 $value = $value->[$indices[$i]];
107 for my $part (@methods) {
108 if (ref($value) =~ m/^(?:Form|HASH)$/) {
109 $value = $value->{$part};
110 } elsif (blessed($value) && $value->can($part)) {
111 $value = $value->$part;
121 sub substitute_vars {
122 my ($self, $text, @indices) = @_;
124 my $form = $self->{"form"};
126 while ($text =~ /$self->{substitute_vars_re}/) {
127 my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
128 my ($var, @option_list) = split(/\s+/, $1);
129 my %options = map { ($_ => 1) } @option_list;
131 my $value = $self->_get_loop_variable($var, 0, @indices);
132 $value = $form->parse_amount({ numberformat => $::myconfig{output_numberformat} || $::myconfig{numberformat} }, $value) if $options{NOFORMAT};
133 $value = $self->format_string($value) unless $options{NOESCAPE};
135 substr($text, $tag_pos, $tag_len, $value);
141 sub _parse_block_if {
142 $main::lxdebug->enter_sub();
145 my $contents = shift;
146 my $new_contents = shift;
150 $$new_contents .= $self->substitute_vars(substr($$contents, 0, $pos_if), @indices);
151 substr($$contents, 0, $pos_if) = "";
153 if ($$contents !~ m/^$self->{tag_start_qm}if
155 (not\b|\!)? # $1 -- Eventuelle Negierung
157 (\b.+?\b) # $2 -- Name der zu überprüfenden Variablen
158 ( # $3 -- Beginn des optionalen Vergleiches
160 ([!=]) # $4 -- Negierung des Vergleiches speichern
161 ([=~]) # $5 -- Art des Vergleiches speichern
163 ( # $6 -- Gequoteter String oder Bareword
165 (.*?)(?<!\\) # $7 -- Gequoteter String -- direkter Vergleich mit eq bzw. ne oder Patternmatching; Escapete Anführungs als Teil des Strings belassen
168 (\b.+?\b) # $8 -- Bareword -- als Index für $form benutzen
174 $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
175 $main::lxdebug->leave_sub();
181 my $comparison = $3; # Optionaler Match um $4..$8
182 my $operator_neg = $4; # '=' oder '!' oder undef, wenn kein Vergleich erkannt
183 my $operator_type = $5; # '=' oder '~' für Stringvergleich oder Regex
184 my $quoted_word = $7; # nur gültig, wenn quoted string angegeben (siehe unten); dann "value" aus <%if var == "value" %>
185 my $bareword = $8; # undef, falls quoted string angegeben wurde; andernfalls "othervar" aus <%if var == othervar %>
187 $not = !$not if ($operator_neg && $operator_neg eq '!');
189 substr($$contents, 0, length($&)) = "";
192 ($block, $$contents) = $self->find_end($$contents, 0, "$var $comparison", $not);
194 $self->{"error"} = "Unclosed $self->{tag_start}if$self->{tag_end}." unless ($self->{"error"});
195 $main::lxdebug->leave_sub();
199 my $value = $self->_get_loop_variable($var, 0, @indices);
202 if ($operator_type) {
203 my $compare_to = $bareword ? $self->_get_loop_variable($bareword, 0, @indices) : $quoted_word;
204 if ($operator_type eq '=') {
205 $hit = ($not && !($value eq $compare_to)) || (!$not && ($value eq $compare_to));
207 $hit = ($not && !($value =~ m/$compare_to/i)) || (!$not && ($value =~ m/$compare_to/i));
211 $hit = ($not && ! $value) || (!$not && $value);
215 my $new_text = $self->parse_block($block, @indices);
216 if (!defined($new_text)) {
217 $main::lxdebug->leave_sub();
220 $$new_contents .= $new_text;
223 $main::lxdebug->leave_sub();