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->{custom_tag_style} = 1;
52 $self->{tag_start} = $tag_start;
53 $self->{tag_end} = $tag_end;
54 $self->{tag_start_qm} = quotemeta $tag_start;
55 $self->{tag_end_qm} = quotemeta $tag_end;
57 $self->{substitute_vars_re} = "$self->{tag_start_qm}(.+?)$self->{tag_end_qm}";
60 sub set_use_template_toolkit {
64 $self->{use_template_toolkit} = $value;
72 # 1. A typeglob for the file handle. The output will be written
73 # to this file handle.
76 # 1 on success and undef or 0 if there was an error. In the latter case
77 # the calling function can retrieve the error message via $obj->get_error()
82 print(OUT "Hallo!\n");
88 return $self->{"error"};
95 sub _get_loop_variable {
96 my ($self, $var, $get_array, @indices) = @_;
97 my $form = $self->{form};
98 my ($value, @methods);
101 ($var, @methods) = split m/\./, $var;
104 if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
105 $value = $form->{TEMPLATE_ARRAYS}->{$var};
107 $value = $form->{$var};
110 for (my $i = 0; $i < scalar(@indices); $i++) {
111 last unless (ref($value) eq "ARRAY");
112 $value = $value->[$indices[$i]];
115 for my $part (@methods) {
116 if (ref($value) =~ m/^(?:Form|HASH)$/) {
117 $value = $value->{$part};
118 } elsif (blessed($value) && $value->can($part)) {
119 $value = $value->$part;
129 sub substitute_vars {
130 my ($self, $text, @indices) = @_;
132 my $form = $self->{"form"};
134 while ($text =~ /$self->{substitute_vars_re}/) {
135 my ($tag_pos, $tag_len) = ($-[0], $+[0] - $-[0]);
136 my ($var, @option_list) = split(/\s+/, $1);
137 my %options = map { ($_ => 1) } @option_list;
139 my $value = $self->_get_loop_variable($var, 0, @indices);
140 $value = $form->parse_amount({ numberformat => $::myconfig{output_numberformat} || $::myconfig{numberformat} }, $value) if $options{NOFORMAT};
141 $value = $self->format_string($value) unless $options{NOESCAPE};
143 substr($text, $tag_pos, $tag_len, $value);
149 sub _parse_block_if {
150 $main::lxdebug->enter_sub();
153 my $contents = shift;
154 my $new_contents = shift;
158 $$new_contents .= $self->substitute_vars(substr($$contents, 0, $pos_if), @indices);
159 substr($$contents, 0, $pos_if) = "";
161 if ($$contents !~ m/^$self->{tag_start_qm}if
163 (not\b|\!)? # $1 -- Eventuelle Negierung
165 (\b.+?\b) # $2 -- Name der zu überprüfenden Variablen
166 ( # $3 -- Beginn des optionalen Vergleiches
168 ([!=]) # $4 -- Negierung des Vergleiches speichern
169 ([=~]) # $5 -- Art des Vergleiches speichern
171 ( # $6 -- Gequoteter String oder Bareword
173 (.*?)(?<!\\) # $7 -- Gequoteter String -- direkter Vergleich mit eq bzw. ne oder Patternmatching; Escapete Anführungs als Teil des Strings belassen
176 (\b.+?\b) # $8 -- Bareword -- als Index für $form benutzen
182 $self->{"error"} = "Malformed $self->{tag_start}if$self->{tag_end}.";
183 $main::lxdebug->leave_sub();
189 my $comparison = $3; # Optionaler Match um $4..$8
190 my $operator_neg = $4; # '=' oder '!' oder undef, wenn kein Vergleich erkannt
191 my $operator_type = $5; # '=' oder '~' für Stringvergleich oder Regex
192 my $quoted_word = $7; # nur gültig, wenn quoted string angegeben (siehe unten); dann "value" aus <%if var == "value" %>
193 my $bareword = $8; # undef, falls quoted string angegeben wurde; andernfalls "othervar" aus <%if var == othervar %>
195 $not = !$not if ($operator_neg && $operator_neg eq '!');
197 substr($$contents, 0, length($&)) = "";
200 ($block, $$contents) = $self->find_end($$contents, 0, "$var $comparison", $not);
202 $self->{"error"} = "Unclosed $self->{tag_start}if$self->{tag_end}." unless ($self->{"error"});
203 $main::lxdebug->leave_sub();
207 my $value = $self->_get_loop_variable($var, 0, @indices);
208 $value = scalar(@{ $value }) if (ref($value) || '') eq 'ARRAY';
211 if ($operator_type) {
212 my $compare_to = $bareword ? $self->_get_loop_variable($bareword, 0, @indices) : $quoted_word;
213 if ($operator_type eq '=') {
214 $hit = ($not && !($value eq $compare_to)) || (!$not && ($value eq $compare_to));
216 $hit = ($not && !($value =~ m/$compare_to/i)) || (!$not && ($value =~ m/$compare_to/i));
220 $hit = ($not && ! $value) || (!$not && $value);
224 my $new_text = $self->parse_block($block, @indices);
225 if (!defined($new_text)) {
226 $main::lxdebug->leave_sub();
229 $$new_contents .= $new_text;
232 $main::lxdebug->leave_sub();