Forum Moderators: coopster & phranque

Message Too Old, No Replies

Using CGI to create dynamic content

Don't want to reinvent the wheel if I don't have to

         

runner

10:36 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



I have a flat file ASCII database that I want to use to create some dynamic content for a web page. I was going to write a cgi script that would read a template file and substitute the variables in the template file with the actual information from the database records; then print out the data.

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.

balam

11:35 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



I'm not so certain you'll find some software straight off the shelf that will suit your purposes, if only for the fact that your database is (probably) in a proprietary format.

> 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.)

runner

3:58 am on Aug 10, 2005 (gmt 0)

10+ Year Member



Thanks balam...

I didn't realize how easy it was when you use HTML::Template

balam

4:29 am on Aug 10, 2005 (gmt 0)

10+ Year Member



My pleasure, but I hope I didn't confuse you. That's not using HTML::Template, that's me re-inventing the wheel. ;)

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")

WWMike

10:26 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Ya, it's actually MUCH easier to re-invent the wheel here since it's pretty much just a read/search/replace/write no-brainer. Here's a sample with 3 tags:

$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;
}

runner

10:46 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



OK... You've all reinvented the wheel for me. I've got 'er working now. Just have to tweak the template so it looks nice. Much easier than I thought.

WWMike

11:03 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Using my example you don't need to tweak at all. Just copy an already manually created index.html as template.html and replace the static text that you want to change with $whatever and run the script. For example:

<TITLE>$title</TITLE>