# Anyone know Perl? Help me with HTML templates?



## Jasoco (Feb 13, 2002)

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:


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


----------



## blb (Feb 13, 2002)

I say head on over to CPAN and find one to your liking (last one I used was HTML::Template, which was quite nice).

If you want a description of several template systems, perl.com ran a nice, long article on this very subject.


----------



## martinatkinson (Mar 18, 2002)

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


----------

