Coding Domain

Perl Modules: CGI::Template


Downloads
Perl Modules
Name
CGI::Template - Easy to use template parser

Synopsis
use CGI::Template;

TemplateLoad('template.html') or die "Can't load: $!";

# Add default a search-replace for %TEMPLATE::COPYRIGHT%
TemplateSetReplace('COPYRIGHT' => $Copyright);

# Prints out until %TEMPLATE::LABEL% is found, and replaces it by 'Hi There!'.
# The label should exist, or else the entire template is printed out first.
print_TemplateUntil('TITLE'    => 'Script using a template');
print_TemplateUntil('TOOLBAR'  => $ToolbarHTML);
print_TemplateUntil('LOCATION' => $LocationHTML);

# Continues Prints the template until the %TEMPLATE::BODY% label
# is found, where out script may print it's HTML output ;-)
print_TemplateUntilLabel('BODY');

print "Hi there! This is my script using a template";
...
More perl code
...
print "End of script HTML code in body part of the HTML";

# Prints the remaining lines of the template
print_TemplateUntilEOF();

exit;

DESCRIPTION
Template parser that can be useful in any CGI script. The functions import a template file, and labels will be used to print out until a certain position. In this way, your CGI script can always look like all your other HTML pages, without the need to hack into the CGI script's code looking for printed lines.

Exported Functions and Variables
TemplateLoad(TemplateFileName)
Loads the template file. The file contents will be saved into the variable @CGI::Template::HTML.

TemplateSetReplace( 'SOMETAG' => 'REPLACEMENT' )
Predefines some labels to be replaced. This could include copyright information and other small repetetive texts, like titles and file paths labels. The replacements will be saved and used when printing the template with one of the functions below.

print_TemplateUntil( 'SOMETAG' => 'REPLACEMENT' )
This will print out the template tekst UNTIL a %TEMPLATE::SOMETAG% is found in the template text. That code will be replaced by the 'REPLACEMENT' value. Any tags found before the 'SOMETAG' will be replaced, if the replacement has been defined by a TemplateSetReplace call. Otherwise, that code will be deleted!

Thus, it's important to have frequently used tags defined before this call. This call is very useful for printing out the template to (for example) <HEAD> tags. Then using normal print commands to print other text that will follow (like <TITLE> and <META> tags), or printing to a %TEMPLATE::FOOTER% location where a generated footer could be printed.

print_TemplateUntilLabel( 'SOMETAG' )
This subroutine works in the same way, with one difference. The tag will be deleted in stead of being replaced. This subroutine is actually a synonym for print_TemplateUntil('SOMETAG' => ''); If you mark a position in the template where the body of the webpage should be printed, this will (for example) print everything until that location.

print_TemplateUntilEOF()
The last subroutine prints out the rest of what is left of the template lines. Every (known) tag that has been defined will be replaced.

$CGI::Template::LABEL
Variable where the text 'TEMPLATE' is saved by default. This is the first part of the template label that should be found, like in %TEMPLATE::SOMETAG%. Change it if you prefer a less original name.

@CGI::Template::HTML
The template codes that are being filled by a TemplateLoad call. You can change these codes at run time.

$CGI::Template::Line
The current line of the template printing. Can be used to modify the template HTML codes array.

Author
Copyright (c) 2001, Diederik van der Boor - All Rights Reserved