]>
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 #
9 # Description: Handle XML based HTML-Templates #
11 ################################################################################
14 use vars
qw($xml_dom_used);
16 use Carp qw(croak confess);
19 $xml_dom_used = eval q
[
26 ### sub new ####################################################################
32 # Return: Template object
37 my $self = bless {} => ref($instance) || $instance;
39 $self -> file
(+shift);
45 ### sub file ###################################################################
47 # assign new template file to object
48 # parse the template file
50 # Params: $new - (optional) new template file
52 # Return: scalar - old filename or if there's no old filename given
57 my $old = $self -> {file
};
59 $self -> {file
} = $new if (defined $new);
66 ### sub insert #################################################################
68 # return the placeholder surrounded by meta delimiters
70 # Params: $name - name of placeholder
72 # Return: scalar - placeholder surrounded by meta delimiters
78 croak
"no template file specified"
79 unless (defined $self -> {file
});
82 $self -> {metaon
} . $name . $self -> {metaoff
};
85 ### sub list ###################################################################
87 # fill in a complete list
89 # Params: $name - name of the atomic scrap
90 # $array - list of hashes (same strcuture like the hash used by 'scrap')
92 # Return: scalar reference - filled in list
98 croak
"no template file specified"
99 unless (defined $self -> {file
});
101 # no warnings 'uninitialized';
102 my $list = join '' => map { ${ $self -> scrap
($name, $_) } } @
{ +shift };
108 ### sub scrap ##################################################################
110 # fill in a template scrap
112 # Params: $name - name of the scrap
114 # Return: scalar reference - filled in scrap
120 croak
"no template file specified"
121 unless (defined $self -> {file
});
123 return \'' unless (defined $name and defined ($self -> {parsed} -> {$name}));
126 # (and normalize - save only the references in %params)
134 ? (defined ${$ref -> {$_}} ? $ref -> {$_} : \'')
135 : \
(defined $ref -> {$_} ?
$ref -> {$_} : ''))
142 my $scrap = $self -> {parsed
} -> {$name};
143 my $qmon = quotemeta $self -> {metaon
};
144 my $qmoff = quotemeta $self -> {metaoff
};
146 # ...until we've replaced all placeholders
155 [ (exists ( $params{$1} ) )
157 : ( exists ( $self -> {parsed
} -> {$1} )
158 ?
$self -> {parsed
} -> {$1}
164 # parse conditional blocks
175 ### sub parse_file #############################################################
177 # read in and parse template file
181 # Return: Status Code (Boolean)
185 my $filename = $self -> {file
};
189 # parse template using XML::DOM
193 new XML
::DOM
::Parser
-> parsefile
($filename);
195 croak
"error while parsing template file '$filename': $@" if ($@
);
197 my $template = $xml -> getElementsByTagName
('Template', 0) -> item
(0);
199 # extract meta delimiters
201 $self -> {metaon
} = $template -> getAttribute
('metaon');
202 $self -> {metaoff
} = $template -> getAttribute
('metaoff');
204 croak
"missing meta defintion(s) in template file '$filename'." unless ($self -> {metaon
} and $self -> {metaoff
});
206 $self -> {parsed
} = {};
207 foreach ($template -> getElementsByTagName
('Scrap', 0)) {
208 my $name = $_ -> getAttribute
('id');
210 croak
"Element 'Scrap' requires attribute 'id' in template file '$filename'." unless (length ($name));
211 croak
"double defined id '$name' in template file '$filename'." if (exists ($self -> {parsed
} -> {$name}));
212 croak
"use '/^[_a-zA-Z]\\S*\$/' for 'Scrap'-ids in template file '$filename' (wrong: '$name')." unless ($name =~ /^[_a-zA-Z]\S*$/);
214 $self -> {parsed
} -> {$name} = $_ -> getFirstChild
-> getData
;
215 $self -> {parsed
} -> {$name} =~ s/^\s+|\s+$//g;}
217 return 1; # looks fine
220 # XML::DOM not available...
221 # parse the template using both hands ;)
224 my ($xml, $root, $template);
227 open FILE
, "< $filename" or croak
"error while reading template file '$filename': $!";
229 close FILE
or croak
"error while closing template file '$filename' after reading: $!";
231 ($root, $template) = ($1, $2) if ($xml =~ m
|(<Template\s
+[^>"]*(?:"[^"]*"[^>"]*)*>)(.*)</Template\s*>|s);
232 croak "error
while parsing template file
'$filename': missing root element
'Template'"
233 unless (defined $root and defined $template);
235 # extract meta delimiters
237 $self -> {metaon} = $1 if ($root =~ /\smetaon\s*=\s*"([^"]+)"/);
238 $self -> {metaoff
} = $1 if ($root =~ /\smetaoff\s*=\s*"([^"]+)"/);
240 croak
"missing meta defintion(s) in template file '$filename'." unless ($self -> {metaon
} and $self -> {metaoff
});
242 # don't use any other entities than " ' < > and &
243 # (while using non XML::DOM - version)
245 for ('metaon', 'metaoff') {
246 $self -> {$_} =~ s/"/"/g; $self -> {$_} =~ s/'/'/g;
247 $self -> {$_} =~ s/</</g; $self -> {$_} =~ s/>/>/g;
248 $self -> {$_} =~ s/&/&/g;
251 $self -> {parsed
} = {};
252 while ($template =~ m
|<Scrap\s
+(?
:id\s
*=\s
*"([^"]+)")?\s*>\s*<!\[CDATA\[([^\]]*(?:\](?!\]>)[^\]]*)*)\]\]>\s*</Scrap\s*>|g) {
254 my ($name, $content) = ($1, $2);
256 croak "Element
'Scrap' requires attribute
'id' in template file
'$filename'"
257 unless (defined $name and length $name);
259 croak "double
defined id
'$name' in template file
'$filename'"
260 if (exists ($self -> {parsed} -> {$name}));
262 croak "use '/^[_a-zA-Z]\\S*\$/' for 'Scrap'-ids
in template file
'$filename' (wrong
: '$name')"
263 unless ($name =~ /^[_a-zA-Z]\S*$/);
265 $content =~ s/^\s+//; $content =~ s/\s+$//;
266 $self -> {parsed} -> {$name} = $content;
269 return 1; # looks fine
272 return; # anything failed (??)
275 ### sub parse_if ###############################################################
277 # parse conditional blocks
279 # Params: $scrap - scalar reference of the template scrap
280 # $params - hash reference: values from the application
282 # Return: ~none~, ($$scrap will be modified)
286 my ($scrap, $params) = @_;
288 my $qmon = quotemeta $self -> {metaon};
289 my $qmoff = quotemeta $self -> {metaoff};
291 # the following regex is just not optimized,
294 1 while ($$scrap =~ s {
295 ($qmon\s*%(?:IF|ELSE)\s+.+?\s*$qmoff.*?) # skip this part
296 (?=$qmon\s*%IF\s+.+?\s*$qmoff) # if %IF or %ELSE are followed by %IF
299 $qmon\s*%IF\s+(.+?)\s*$qmoff # %IF
302 $qmon\s*%ENDIF\s*$qmoff # followed by %ENDIF
304 $qmon\s*%ELSE\s*$qmoff # %ELSE...
306 $qmon\s*%ENDIF\s*$qmoff # ...and ENDIF
312 my ($t3, $t4, $t5) = ($3, $4, $5);
314 for (split /\s+/,$t3) {
316 exists($params->{$_})
317 and defined ${$params->{$_}}
318 and length ${$params->{$_}}
324 $ret = $t5 || '' unless (defined $ret);
336 # keeping 'require' happy
341 ### end of Template ############################################################
patrick-canterino.de