]>
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-06-16 #
8 # Frank Schoenmann <fs@tower.de>, 2001-06-04 #
10 # Description: Handle XML based HTML-Templates #
12 ################################################################################
26 $xml_dom_used = eval q
[
33 ################################################################################
37 $VERSION = do { my @r =(q
$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x
$#r, @r };
40 ### sub new ####################################################################
46 # Return: Template object
51 my $self = bless {} => ref($instance) || $instance;
53 $self -> file
(+shift);
59 ### sub file ###################################################################
61 # assign new template file to object
62 # parse the template file
64 # Params: $new - (optional) new template file
66 # Return: scalar - old filename or if there's no old filename given
71 my $old = $self -> {file
};
73 $self -> {file
} = $new if (defined $new);
80 ### sub insert #################################################################
82 # return the placeholder surrounded by meta delimiters
84 # Params: $name - name of placeholder
86 # Return: scalar - placeholder surrounded by meta delimiters
92 croak
"no template file specified"
93 unless (defined $self -> {file
});
96 $self -> {metaon
} . $name . $self -> {metaoff
};
99 ### sub list ###################################################################
101 # fill in a complete list
103 # Params: $name - name of the atomic scrap
104 # $array - list of hashes (same strcuture like the hash used by 'scrap')
106 # Return: scalar reference - filled in list
112 croak
"no template file specified"
113 unless (defined $self -> {file
});
115 # no warnings 'uninitialized';
116 my $list = join '' => map { ${ $self -> scrap
($name, $_) } } @
{ +shift };
122 ### sub scrap ##################################################################
124 # fill in a template scrap
126 # Params: $name name of the scrap
128 # $no_nl 1 - remove newlines (\n)
129 # 0 - do no such thing
131 # Return: scalar reference - filled in scrap
142 croak
"no template file specified"
143 unless (defined $self -> {file
});
145 return \'' unless (defined $name and defined ($self -> {parsed} -> {$name}));
148 # (and normalize - save only the references in %params)
156 ? (defined ${$ref -> {$_}} ? $ref -> {$_} : \'')
157 : \
(defined $ref -> {$_} ?
$ref -> {$_} : ''))
164 my $scrap = $self -> {parsed
} -> {$name};
165 my $qmon = quotemeta $self -> {metaon
};
166 my $qmoff = quotemeta $self -> {metaoff
};
168 # ...until we've replaced all placeholders
177 [ (exists ( $params{$1} ) )
179 : ( exists ( $self -> {parsed
} -> {$1} )
180 ?
$self -> {parsed
} -> {$1}
186 # parse conditional blocks
195 $scrap =~ s/\015\012|\015|\012//g if ($no_nl);
201 ### sub parse_file #############################################################
203 # read in and parse template file
207 # Return: Status Code (Boolean)
211 my $filename = $self -> {file
};
215 # parse template using XML::DOM
219 new XML
::DOM
::Parser
-> parsefile
($filename);
221 croak
"error while parsing template file '$filename': $@" if ($@
);
223 my $template = $xml -> getElementsByTagName
('Template', 0) -> item
(0);
225 # extract meta delimiters
227 $self -> {metaon
} = $template -> getAttribute
('metaon');
228 $self -> {metaoff
} = $template -> getAttribute
('metaoff');
230 croak
"missing meta defintion(s) in template file '$filename'." unless ($self -> {metaon
} and $self -> {metaoff
});
232 $self -> {parsed
} = {};
233 foreach ($template -> getElementsByTagName
('Scrap', 0)) {
234 my $name = $_ -> getAttribute
('id');
236 croak
"Element 'Scrap' requires attribute 'id' in template file '$filename'." unless (length ($name));
237 croak
"double defined id '$name' in template file '$filename'." if (exists ($self -> {parsed
} -> {$name}));
238 croak
"use '/^[_a-zA-Z]\\S*\$/' for 'Scrap'-ids in template file '$filename' (wrong: '$name')." unless ($name =~ /^[_a-zA-Z]\S*$/);
240 $self -> {parsed
} -> {$name} = $_ -> getFirstChild
-> getData
;
241 $self -> {parsed
} -> {$name} =~ s/^\s+|\s+$//g;}
243 return 1; # looks fine
246 # XML::DOM not available...
247 # parse the template using both hands ;)
250 my ($xml, $root, $template);
253 open FILE
, "< $filename" or croak
"error while reading template file '$filename': $!";
255 close FILE
or croak
"error while closing template file '$filename' after reading: $!";
257 ($root, $template) = ($1, $2) if ($xml =~ m
|(<Template\s
+[^>"]*(?:"[^"]*"[^>"]*)*>)(.*)</Template\s*>|s);
258 croak "error
while parsing template file
'$filename': missing root element
'Template'"
259 unless (defined $root and defined $template);
261 # extract meta delimiters
263 $self -> {metaon} = $1 if ($root =~ /\smetaon\s*=\s*"([^"]+)"/);
264 $self -> {metaoff
} = $1 if ($root =~ /\smetaoff\s*=\s*"([^"]+)"/);
266 croak
"missing meta defintion(s) in template file '$filename'." unless ($self -> {metaon
} and $self -> {metaoff
});
268 # don't use any other entities than " ' < > and &
269 # (while using non XML::DOM - version)
271 for ('metaon', 'metaoff') {
272 $self -> {$_} =~ s/"/"/g; $self -> {$_} =~ s/'/'/g;
273 $self -> {$_} =~ s/</</g; $self -> {$_} =~ s/>/>/g;
274 $self -> {$_} =~ s/&/&/g;
277 $self -> {parsed
} = {};
278 while ($template =~ m
|<Scrap\s
+(?
:id\s
*=\s
*"([^"]+)")?\s*>\s*<!\[CDATA\[([^\]]*(?:\](?!\]>)[^\]]*)*)\]\]>\s*</Scrap\s*>|g) {
280 my ($name, $content) = ($1, $2);
282 croak "Element
'Scrap' requires attribute
'id' in template file
'$filename'"
283 unless (defined $name and length $name);
285 croak "double
defined id
'$name' in template file
'$filename'"
286 if (exists ($self -> {parsed} -> {$name}));
288 croak "use '/^[_a-zA-Z]\\S*\$/' for 'Scrap'-ids
in template file
'$filename' (wrong
: '$name')"
289 unless ($name =~ /^[_a-zA-Z]\S*$/);
291 $content =~ s/^\s+//; $content =~ s/\s+$//;
292 $self -> {parsed} -> {$name} = $content;
295 return 1; # looks fine
298 return; # anything failed (??)
301 ### sub parse_if ###############################################################
303 # parse conditional blocks
305 # Params: $scrap - scalar reference of the template scrap
306 # $params - hash reference: values from the application
308 # Return: ~none~, ($$scrap will be modified)
312 my ($scrap, $params) = @_;
314 my $qmon = quotemeta $self -> {metaon};
315 my $qmoff = quotemeta $self -> {metaoff};
317 # the following regex is just not optimized,
320 1 while ($$scrap =~ s {
321 ($qmon\s*%(?:IF|ELSE)\s+.+?\s*$qmoff.*?) # skip this part
322 (?=$qmon\s*%IF\s+.+?\s*$qmoff) # if %IF or %ELSE are followed by %IF
325 $qmon\s*%IF\s+(.+?)\s*$qmoff # %IF
328 $qmon\s*%ENDIF\s*$qmoff # followed by %ENDIF
330 $qmon\s*%ELSE\s*$qmoff # %ELSE...
332 $qmon\s*%ENDIF\s*$qmoff # ...and ENDIF
338 my ($t3, $t4, $t5) = ($3, $4, $5);
340 for (split /\s+/,$t3) {
342 exists($params->{$_})
343 and defined ${$params->{$_}}
344 and length ${$params->{$_}}
350 $ret = $t5 || '' unless (defined $ret);
362 # keep 'require' happy
367 ### end of Template ############################################################
patrick-canterino.de