NAME

Template::Recall - "Reverse callback" templating system

SYNOPSIS

use Template::Recall;

my $tr = Template::Recall->new( '/path/to/template/sections' );

my @prods = (
	'soda,sugary goodness,$.99', 
	'energy drink,jittery goodness,$1.99',
	'green tea,wholesome goodness,$1.59'
	);

$tr->render('header');

# Load template into memory

$tr->preload('prodrow');
						
for (@prods) 
{
	my %h;
	my @a = split(/,/, $_);

	$h{'TEMPLATE_var_product'} = $a[0];
	$h{'TEMPLATE_var_description'} = $a[1];
	$h{'TEMPLATE_var_price'} = $a[2];

	print $tr->render('prodrow', %h);
}

# Remove template from memory

$tr->unload('prodrows');

print $tr->render('footer');

DESCRIPTION

Template::Recall works using what I call a "reverse callback" approach. A "callback" templating system (i.e. Mason, Apache::ASP) generally includes template markup and code in the same file. The template "calls" out to the code where needed. Template::Recall works in reverse. Rather than inserting code inside the template, the template remains separate, but broken into sections. The sections are called from within the code at the appropriate times.

A template section is merely a file on disk (or a "marked" section in a single file). For instance, 'prodrow' above (actually prodrow.html in the template directory), looks like

<tr>
	<td>TEMPLATE_var_product</td>
	<td>TEMPLATE_var_description</td>
	<td>TEMPLATE_var_price</td>
</tr>

The render() method is used to "call" out to the template sections. Simply create a hash of name/value pairs that represent the template tags you wish to replace, and pass it along with the template section, i.e.

$tr->render('prodrow', %h);

METHODS

new( [$template_path] [, flavor => $template_flavor] [, secpat=> $section_pattern ] )

Instantiates the object. If you do not specify $template_path, it will assume the diretory that the script lives in. If $template_path points to a file rather than a directory, it loads all the template sections from this file. The file must be sectioned using the "section pattern", which can be adjusted via secpat.

flavor is a pattern to specify what type of template to load. This is /html$|htm$/i by default, which picks up HTML file extensions. You could set it to /xml$/i, for instance, to get *.xml files.

secpat, by default, is /<%\s*RECALLSEC_\w+\s*%>/. So if you put all your template sections in one file, the way Template::Recall knows where to get the sections is via this pattern, e.g.

<% RECALLSEC_header %>
<html
	<head><title>Untitled</title></head>
<body>

<table>

<% RECALLSEC_prodrow %>
<tr>
	<td>TEMPLATE_var_product</td>
	<td>TEMPLATE_var_description</td>
	<td>TEMPLATE_var_price</td>
</tr>

<% RECALLSEC_footer %>

</table>

</body>
</html>

You may set secpat to any pattern you wish.

render( $template_pattern [, %hash_of_replacements ] );

You must specify $template_pattern, which tells render() what template "section" to load. %hash_of_replacements is optional. Sometimes you just want to return a template section without any variables. Usually, %hash_of_replacements will be used, and render() iterates through the hash, replacing the key found in the template with the value associated to key.

preload( $template_pattern );

In the loop over @prods in the synopsis, the 'prodrow' template is being accessed multiple times. If the section is stored in a file, i.e. prodrow.html, you have to read from the disk every time render() is called. preload() allows you to load a template section file into memory. Then, every time render() is called, it pulls the template from memory rather than disk. This does not work for single file templates, since they are already loaded into memory.

unload( $template_pattern );

When you are finished with the template, free up the memory.

AUTHOR

James Robson <info AT arbingersys DOT com

SEE ALSO

http://perl.apache.org/docs/tutorials/tmpl/comparison/comparison.html