Anyone know Perl? Help me with HTML templates?

Jasoco

Video Gamer/Collector
Ok, on my site I want to have the same layout each page with the content inside.

I want to have an HTML file external on the site somewhere and have the Perl files, while writing the page, load the template.html file and use the layout inside as the layout for each page.

Say I'd write an HTML file like this:

Code:
<HTML>
	<HEAD>
		<TITLE><!--## Insert Title Here ##--></TITLE>
	</HEAD>
	<BODY>
		<TABLE>
			<TR>
				<TD>
					<!--## Navigation ##-->
				</TD>
				<TD>
					<!--## Content of current page ##-->
				</TD>
			</TR>
		</TABLE>
	</BODY>
</HEAD>

And I'd use the Perl file to fill in the comments with what is supposed to go there.

I know it's possible, I just don't know how. I wanna know how to do it.

And don't criticize me for using Perl. ;)
 
Hello!

Say that you marked all the insertion points in your html template thus:

<!--INSERT_NAME-->
<!--INSERT_ADDRESS-->
<!--INSERT_STATE-->
etc. where $NAME, $ADDRESS, and $STATE were the names of variables in your script.

Then your script could look for the insert statements, pull the names from each, and substitute their variables, thus:

# Open the HTML file #
open(IN,"/path/to/page.html");
@lines = <IN>;
close(IN);

# Print the header #
print "Content-type: text/html\n\n";

# Print the HTML file #
foreach $line(@lines) {
if($line =~ /<!--INSERT_(\w+)-->/){
$line = $$1;
}
print "$line";
}

Note that your HTML file must have UNIX line breaks for this to work. Also, your comments must be on a line of their own since when the cgi finds the comment it will replace the whole line.

Have a great day!

Albert
 
Back
Top