]>
git.p6c8.net - selfforum.git/blob - selfforum-cgi/shared/Template.pm
3 ################################################################################
5 # File: shared/Template.pm #
7 # Authors: André Malo <nd@o3media.de>, 2001-04-12 #
8 # Frank Schoenmann <fs@tower.de>, 2001-06-04 #
10 # Description: Handle XML based HTML-Templates #
12 ################################################################################
15 use vars
qw($xml_dom_used);
17 use Carp qw(croak confess);
20 $xml_dom_used = eval q
[
27 ### sub new ####################################################################
33 # Return: Template object
38 my $self = bless {} => ref($instance) || $instance;
40 $self -> file
(+shift);
46 ### sub file ###################################################################
48 # assign new template file to object
49 # parse the template file
51 # Params: $new - (optional) new template file
53 # Return: scalar - old filename or if there's no old filename given
58 my $old = $self -> {file
};
60 $self -> {file
} = $new if (defined $new);
67 ### sub insert #################################################################
69 # return the placeholder surrounded by meta delimiters
71 # Params: $name - name of placeholder
73 # Return: scalar - placeholder surrounded by meta delimiters
79 croak
"no template file specified"
80 unless (defined $self -> {file
});
83 $self -> {metaon
} . $name . $self -> {metaoff
};
86 ### sub list ###################################################################
88 # fill in a complete list
90 # Params: $name - name of the atomic scrap
91 # $array - list of hashes (same strcuture like the hash used by 'scrap')
93 # Return: scalar reference - filled in list
99 croak
"no template file specified"
100 unless (defined $self -> {file
});
102 # no warnings 'uninitialized';
103 my $list = join '' => map { ${ $self -> scrap
($name, $_) } } @
{ +shift };
109 ### sub scrap ##################################################################
111 # fill in a template scrap
113 # Params: $name name of the scrap
115 # $no_nl 1 - remove newlines (\n)
116 # 0 - do no such thing
118 # Return: scalar reference - filled in scrap
129 croak
"no template file specified"
130 unless (defined $self -> {file
});
132 return \'' unless (defined $name and defined ($self -> {parsed} -> {$name}));
135 # (and normalize - save only the references in %params)
143 ? (defined ${$ref -> {$_}} ? $ref -> {$_} : \'')
144 : \
(defined $ref -> {$_} ?
$ref -> {$_} : ''))
151 my $scrap = $self -> {parsed
} -> {$name};
152 my $qmon = quotemeta $self -> {metaon
};
153 my $qmoff = quotemeta $self -> {metaoff
};
155 # ...until we've replaced all placeholders
164 [ (exists ( $params{$1} ) )
166 : ( exists ( $self -> {parsed
} -> {$1} )
167 ?
$self -> {parsed
} -> {$1}
173 # parse conditional blocks
181 $scrap =~ s/\n|\r\n|\n\r|\r//g if ($no_nl);
187 ### sub parse_file #############################################################
189 # read in and parse template file
193 # Return: Status Code (Boolean)
197 my $filename = $self -> {file
};
201 # parse template using XML::DOM
205 new XML
::DOM
::Parser
-> parsefile
($filename);
207 croak
"error while parsing template file '$filename': $@" if ($@
);
209 my $template = $xml -> getElementsByTagName
('Template', 0) -> item
(0);
211 # extract meta delimiters
213 $self -> {metaon
} = $template -> getAttribute
('metaon');
214 $self -> {metaoff
} = $template -> getAttribute
('metaoff');
216 croak
"missing meta defintion(s) in template file '$filename'." unless ($self -> {metaon
} and $self -> {metaoff
});
218 $self -> {parsed
} = {};
219 foreach ($template -> getElementsByTagName
('Scrap', 0)) {
220 my $name = $_ -> getAttribute
('id');
222 croak
"Element 'Scrap' requires attribute 'id' in template file '$filename'." unless (length ($name));
223 croak
"double defined id '$name' in template file '$filename'." if (exists ($self -> {parsed
} -> {$name}));
224 croak
"use '/^[_a-zA-Z]\\S*\$/' for 'Scrap'-ids in template file '$filename' (wrong: '$name')." unless ($name =~ /^[_a-zA-Z]\S*$/);
226 $self -> {parsed
} -> {$name} = $_ -> getFirstChild
-> getData
;
227 $self -> {parsed
} -> {$name} =~ s/^\s+|\s+$//g;}
229 return 1; # looks fine
232 # XML::DOM not available...
233 # parse the template using both hands ;)
236 my ($xml, $root, $template);
239 open FILE
, "< $filename" or croak
"error while reading template file '$filename': $!";
241 close FILE
or croak
"error while closing template file '$filename' after reading: $!";
243 ($root, $template) = ($1, $2) if ($xml =~ m
|(<Template\s
+[^>"]*(?:"[^"]*"[^>"]*)*>)(.*)</Template\s*>|s);
244 croak "error
while parsing template file
'$filename': missing root element
'Template'"
245 unless (defined $root and defined $template);
247 # extract meta delimiters
249 $self -> {metaon} = $1 if ($root =~ /\smetaon\s*=\s*"([^"]+)"/);
250 $self -> {metaoff
} = $1 if ($root =~ /\smetaoff\s*=\s*"([^"]+)"/);
252 croak
"missing meta defintion(s) in template file '$filename'." unless ($self -> {metaon
} and $self -> {metaoff
});
254 # don't use any other entities than " ' < > and &
255 # (while using non XML::DOM - version)
257 for ('metaon', 'metaoff') {
258 $self -> {$_} =~ s/"/"/g; $self -> {$_} =~ s/'/'/g;
259 $self -> {$_} =~ s/</</g; $self -> {$_} =~ s/>/>/g;
260 $self -> {$_} =~ s/&/&/g;
263 $self -> {parsed
} = {};
264 while ($template =~ m
|<Scrap\s
+(?
:id\s
*=\s
*"([^"]+)")?\s*>\s*<!\[CDATA\[([^\]]*(?:\](?!\]>)[^\]]*)*)\]\]>\s*</Scrap\s*>|g) {
266 my ($name, $content) = ($1, $2);
268 croak "Element
'Scrap' requires attribute
'id' in template file
'$filename'"
269 unless (defined $name and length $name);
271 croak "double
defined id
'$name' in template file
'$filename'"
272 if (exists ($self -> {parsed} -> {$name}));
274 croak "use '/^[_a-zA-Z]\\S*\$/' for 'Scrap'-ids
in template file
'$filename' (wrong
: '$name')"
275 unless ($name =~ /^[_a-zA-Z]\S*$/);
277 $content =~ s/^\s+//; $content =~ s/\s+$//;
278 $self -> {parsed} -> {$name} = $content;
281 return 1; # looks fine
284 return; # anything failed (??)
287 ### sub parse_if ###############################################################
289 # parse conditional blocks
291 # Params: $scrap - scalar reference of the template scrap
292 # $params - hash reference: values from the application
294 # Return: ~none~, ($$scrap will be modified)
298 my ($scrap, $params) = @_;
300 my $qmon = quotemeta $self -> {metaon};
301 my $qmoff = quotemeta $self -> {metaoff};
303 # the following regex is just not optimized,
306 1 while ($$scrap =~ s {
307 ($qmon\s*%(?:IF|ELSE)\s+.+?\s*$qmoff.*?) # skip this part
308 (?=$qmon\s*%IF\s+.+?\s*$qmoff) # if %IF or %ELSE are followed by %IF
311 $qmon\s*%IF\s+(.+?)\s*$qmoff # %IF
314 $qmon\s*%ENDIF\s*$qmoff # followed by %ENDIF
316 $qmon\s*%ELSE\s*$qmoff # %ELSE...
318 $qmon\s*%ENDIF\s*$qmoff # ...and ENDIF
324 my ($t3, $t4, $t5) = ($3, $4, $5);
326 for (split /\s+/,$t3) {
328 exists($params->{$_})
329 and defined ${$params->{$_}}
330 and length ${$params->{$_}}
336 $ret = $t5 || '' unless (defined $ret);
348 # keeping 'require' happy
353 ### end of Template ############################################################
patrick-canterino.de