####
my %seg;
my $content = ExtractAllSegments(ReadFile("template.html"), \%seg);
####
my $options;
foreach my $form_label (sort(keys(%form_elements)_ {
my $element = $seg{"form_option"};
ReplaceVariable (\$element, "label", $form_label);
ReplaceVariable (\$element, "value", $form_elements{$form_label});
$options .= $element;
}
InsertAtPlaceholder(\$content, "form_option", $options);
####
#####
# HTML Template System
sub ExtractAllSegments {
#USAGE: $remaining_content = ExtractAllSegments ($template_content, \%segment_hash)
#where $template_content is a template with segments;
# $remaining_content is where the template without segments will be returned
# and \%segment_hash is a pointer to the hash where the segments will go
#
#Mark segments like this: ...
#Mark segment placeholders (for copies of a segment) like this:
#Mark variables like this: {[variable_name]}
#segments, variables and placeholders are case sensitive!
my ($content, $hash) = @_;
my $count = 0;
while ($content =~ /^.*\n?(.*?)<\/segment>.*?$/s) {
$count ++;
if ($count > 1000) {return $content;} #Don't let it loop forever if something goes wrong!
my $name = $1;
my $data = $2;
$content =~ s/.*?<\/segment>/<\!-- placeholder $name -->/s;
$$hash{$name} = $data;
}
return $content;
}
sub ReplaceVariable {
#USAGE: ReplaceVariable (\$content, $variable_name, $variable_content);
my ($content, $name, $insert) = @_;
$$content =~ s/\{$name\}/$insert/g;
}
sub InsertAtPlaceholder {
#USAGE: InsertAtPlaceholder (\$content, $placeholder_name, $content_to_insert);
my ($content, $name, $insert) = @_;
$$content =~ s/<\!-- placeholder $name -->/$insert/g;
}
sub FlushPlaceholders {
#USAGE: FlushPlaceholders(\$content)
my $content = $_[0];
$$content =~ s/<\!-- placeholder \w* -->//g;
$$content =~ s/\n\n\n+/\n\n/g;
}
# End HTML Template System
#####
# File Access Routines
sub ReadFile {
# USAGE: $file_contents = ReadFile($file_name);
unless (open(FILE,"< $_[0]")) {return 0;}
my $readin = join "", ;
close FILE;
$readin =~ s/\r//g;
return ($readin);
}
# End File Access Routines
#####