James Robson > Template-Recall-0.21 > Template::Recall



Annotate this POD

Related Modules

By perlmonks.org
View/Report Bugs
Module Version: 0.21   Source  


Template::Recall - "Reverse callback" templating system


        use Template::Recall;

        # Load template sections from file
        my $tr = Template::Recall->new( template_path => '/path/to/template_file.htmt' );

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

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

        for (@prods)
                my %h;
                my @a = split(/,/, $_);

                $h{'product'} = $a[0];
                $h{'description'} = $a[1];
                $h{'price'} = $a[2];

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

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


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, such as prodrow above, looks something like

    [=== prodrow ===]
                <td>[' product ']</td>
                <td>[' description ']</td>

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

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


new( [ template_path => $path, secpat => $section_pattern, delims => ['opening', 'closing'] ] )

Instantiates the object. You must pass either template_path or template_str as the first parameter.

secpat, by default, is qr/[\s*=+\s*(\w+)\s*=+\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.

        [ ==================== header ==================== ]


        [ ==================== prodrow ==================== ]
                <td>[' product ']</td>
                <td>[' description ']</td>
                <td>[' price ']</td>

        [==================== footer ==================== ]



You may set secpat to any pattern you wish, but you probably shouldn't unless you have some legitimate reason to do so. It needs to be a compiled regex too: qr//.

The default delimeters for variables in Template::Recall are [' (opening) and '] (closing). This tells Template::Recall that [' price '] is different from "price" in the same template, e.g.

        What is the price? It's [' price ']

You can change delims by passing a two element array to new() representing the opening and closing delimiters, such as delims => [ '<%', '%>' ]. If you don't want to use delimiters at all, simply set delims => 'none'.

The template_str parameter allows you to pass in a string that contains the template data, instead of reading it from disk:

new( template_str => $str )

For example, this enables you to store templates in the __DATA__ section of the calling script

render( $section [, $reference_to_hash ] );

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

trim( 'off|left|right|both' );

You may want to control whitespace in your section output. You could use s/// on the returned text, of course, but trim()is included for convenience and clarity. Simply pass the directive you want when you call it, e.g.

        print $tr->render('sec1', \%values);
        print $tr->render('sec2', \%values2);
        # ...etc...

If you just do


it will default to trimming both ends of the template. Note that you can also use abbreviations, i.e. $tr->trim( 'o|l|r|b' ) to save a few keystrokes.


James Robson <arbingersys AT gmail DOT com>


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

Tutorial -- http://www.perl.com/pub/2008/03/14/reverse-callback-templating.html

Performance comparison -- http://soundly.me/template-recall-vs-template-toolkit

syntax highlighting: