February 27, 2021
Hot Topics:

Using templates with Perl/CGI

  • By Dave Edis
  • Send Email »
  • More Articles »

Here's how it works. The regexp searches for variable names that starts with a "$" followed by one or more "word characters." In the regexp, "\w" means a "word character" (a-z,A-Z,0-9 or an underscore) and "+" means one or more times. The brackets around "\w+" tell it to remember what variable name was that was matched and save it in "$1." And finally, "${$1}" is evaluated to find the value of that variable. Remember, the name of the variable is stored in $1. And here's how you use it in your program.

### Display Login Screen 
print "Content-type: text/html\n\n"; 
print &Template("_webmail_login.html");  

So now, even though your HTML is stored in templates files outside of your program, variables will be translated just like they would in Perl.

Complex Templates and HTML Fragments

Now, there some situations where just using a single template won't work such as a search engine where you need to return a different number of results based on what was found, or a weather page where you may want to display different images and text based on the forecast. In cases like these you can use multiple template files to make up the screen. For example, you can divide the search engine results page up into sections:

_search_results_header.htmlf       # page header 
_search_results_result.htmlf       # result row 
_search_results_footer.htmlf       # page footer  

These sections are sometimes called HTML fragments because it's really just a fragment of a full HTML page. Giving the HTML fragments a different file extension ".htmlf" helps to distinguish them from full HTML files. By breaking your HTML page up into fragments you can have your program print as many search results as are required.

print &Template("_search_results_header.htmlf"); 
foreach $result (@results) { 
    print &Template("_search_results_result.htmlf"); 
print &Template("_search_results_footer.htmlf");  

For our weather page example you can use HTML Fragments as well, but instead of printing them one after another you can load the HTML fragments that are required and display them inside of another page. Say we have our main weather page template _weather.html and where we want to insert the three day forecast we enter the variables "$day1 $day2 $day3." Now we'll create a small HTML fragment file for each possible weather condition and then load them as needed:

### Load HTML fragments for weather forecast 
$day1 = &Template("_weather_sunny.htmlf"); 
$day2 = &Template("_weather_cloudy.htmlf"); 
$day3 = &Template("_weather_snowing.htmlf"); 

print &Template("_weather.html");  

Where to go from here

One of the things that may take some getting used for designers is having all those funny looking variables in the HTML files (i.e., "Today is $date"). Some people find it easier to use another symbol (or format) other than a preceding "$" to identify variables. With a few modifications you can easily have your program use "%variable%" or "__variable__" or some other variant if that makes more sense to you.

Even if this is the first time you've thought about using templates instead of in-line HTML in your programs, I think you'll find it's easy and fast to switch over and that templates make a lot of sense.

Dave Edis is president of Edis Digital, a new media solutions firm in Vancouver, Canada. Edis Digital develops Web-based publishing tools and applications for companies around the world.

Page 2 of 2

This article was originally published on December 14, 1998

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date