I've been reading the O'Reilly book "CGI Programming with Perl." After reading Chapter 6: HTML Templates, I realized the script I need is so generic that I should to be able to find something already made for this purpose.
Anybody know of a generic script, either commercial or open source that will allow me to define a data source, a template and then do the substitutions to create the content? Seems like a waste to write something like this from scratch.
> Seems like a waste to write something like this from scratch.
Depends on your prospective... For small stuff like this, I always re-invent the wheel and start from scratch - it makes me a better programmer. (Learning through experience, blah-blah-blah...)
That said, here's a subroutine (that is essentially copied 'n' pasted from a working program) that reads a template, fills in the variables, and spits it out to either your visitor or to your main program for further processing:
## &Output_HTML_Template('filepath_to_webroot','template_path_and_filename'[,1])
## - Outputs a HTML template file to browser
## - Add ",1" to return output rather than send to browser
##
## Template:
##- In HTML, position template vars with: <% variable %>
##- In perl, set template vars with: %template_var ($template_var{'variable'} = "Replacement variable string")
##
sub Output_HTML_Template {
my($root_dir,$template,$no_browser) = @_;
my($webpage);# Open template
open (TEMPLATE, "$root_dir$template") ¦¦ die "Template file is missing: $!";# Step line-by-line through template
while (<TEMPLATE>) {
my($en_var,$filename,$include_line,$included,$line,$random,@stat,$tag,$time);
chomp;
$line = $_;# Check line for template tags: <% foo %>
TAG: {
# Look for tag
if ($line =~ /<% (\w+) %>/) {
$tag = $1;
# Replace recognized tag...
if ($template_var{$tag}) {
$line =~ s/<% $tag %>/$template_var{$tag}/g;
redo TAG;
}
# ...or delete unknown tag
else {
$line =~ s/<% $tag %>//g;
redo TAG;
}}}# Add line to output webpage
$webpage .= $line . "\n";
}# Close template
close (TEMPLATE);# Output template
unless ($no_browser) {
print <<WEBPAGE;
Content-type: text/html$webpage
WEBPAGE
}
else { return $webpage }
}
As mentioned, this is C'n'P'ed from another program and does not qualify as good production code. I didn't go over this with a fine-toothed comb; I post it as an example of how to parse a template. (There are a few variables declared that aren't used - they're for the section of the sub (that I've removed) that parses SSI's in templates.)
It's quick, a little dirty, and looks terrible when formatted by WebmasterWorld, but it gets the job done quickly. If I set the variable...
$template_var{'message'} = 'Hello World!';
...and have a template like...
<html><head><title>TESTING!</title></head><body><% message %></body></html>
...I output this:
<html><head><title>TESTING!</title></head><body>Hello World!</body></html>
To be honest, you're the one with the book, so you are the one who knows which works better for you (or if you find some existing software) - I haven't played with HTML::Template.
Since TMTOWTDI*, I like to roll my own... :)
("Tim-toady" - "There's More Than One Way To Do It")
$name = 'Joe Blowe';
$address = '123 Main Street';
$citystatezip = 'Bumphuk, Egypt 12345';
open ( INDEXPAGE, '>index.html' );
open ( TEMPLATE, 'template.html' );
while ( <TEMPLATE> ) {
&translate;
print INDEXPAGE $line;
}
close ( TEMPLATE );
close ( INDEXPAGE );
sub translate {
$line = $_;
$line =~ s/\$name/$name/ego;
$line =~ s/\$address/$address/ego;
$line =~ s/\$citystatezip/$citystatezip/ego;
}