use Text::Template 'fill_in_file';
my $TEMPLATE= 'my.templ';
# ...
my $filled_template= fill_in_file( $TEMPLATE);
# I usually use the HASH option to separate variables that
# go in the template from variables in the code
# my $filled_template= fill_in_file( $TEMPLATE,
# HASH => { title => $title,
# fields => \%fields
# }
# );
print $filled_template;
In the template, variables (or any Perl code) are delimited by { ... }, you can use alternate delimiters if you want (it will be a little faster but the delimiter should not appear in the embedded code), use the argument DELIMITERS => [$open, $close]:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"
"http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<title>My Kewl Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"
+>
</head>
<body>
<h1>{$title}</h1>
<p>Field1: {$field{field1}}</p>
{ "<p>Field2: $field{field2}</p>" if( $field{field2} }
</body>
</html>
|