This question seems a little confused ... but if I am summarising what it is you are wanting to achieve correctly - You want to take HTML from a file and output that with variables which you have stored elsewhere - To achieve such a task, you don't need CGI.pm as much elegant solutions existing using
HTML::Template and
HTML::Mason.
An example of how to achieve what (I think) you are wanting to achieve with HTML::Template follows:
#!/usr/bin/perl
use CGI;
use HTML::Template;
use strict;
# Define the colour of the background variable, $bgcolor
#
my ($bgcolor) = "000000";
# Create CGI object
#
my ($cgi) = CGI->new();
# Create HTML::Template object, use template.html
#
my ($html) = HTML::Template->new(
filename => 'template.html'
);
# Substitute the value of $bgcolor into template.html
#
$html->param(
'bgcolor' => $bgcolor,
);
# Print the output
#
print STDOUT $cgi->header(), $html->output;
exit 0;
... and the HTML::Template file, template.html ...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:/
+/www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>An example template file</title>
</head>
<body bgcolor="#<tmpl_var name="bgcolor">">
</body>
</html>
Now obviously, this is a very simple example of how you can interpolate variables into output HTML, but
HTML::Template (and its contempararies) offer some very powerful ways to generate HTML from programming elements and thus help to separate these elements for later review and maintenance.
The CGI module too has a role in such a system, but less so using its HTML output functions, given the handling of these from the template HTML files (which you seemed to infer to) and the HTML::Template module. The CGI module can still play a big role in a dynamic HTML/CGI application (
not DHTML/CGI :) system as the method by which CGI passed parameters can be acted upon by your Perl script.
For example, you could add in a line to the Perl script above, to have the background colour set via a CGI passed variable in the form of bgcolor=<value> in the query string, as follows:
$bgcolor = ($cgi->param('bgcolor') =~ /^[\da-fA-F]{6}$/) ? $cgi->param
+('bgcolor') : $bgcolor;
This line inserted after the
my ($cgi) = CGI->new(); would also allow the bgcolor variable in the template file to be set to a value passed by CGI.
This is my poor attempt to answer your question ... but I hope I have addressed at least part of what you ask and given you something to play with and/or think about.
Ooohhh, Rob no beer function well without!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.