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

Download:
Template-Recall-0.21.tar.gz

Dependencies

Annotate this POD

Related Modules

HTML::Template
Template::Alloy
Text::Template
CGI::Application
Data::Dumper
HTML::FillInForm
Data::DRef
Apache::ASP
more...
By perlmonks.org
View/Report Bugs
Module Version: 0.21   Source  

NAME ^

Template::Recall - "Reverse callback" templating system

SYNOPSIS ^

        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');

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

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

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);

METHODS ^

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 ==================== ]
        <html
                <head><title>Untitled</title></head>
        <body>

        <table>

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

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

        </table>

        </body>
        </html>

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.

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

If you just do

        $tr->trim();

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.

AUTHOR ^

James Robson <arbingersys AT gmail DOT com>

SEE ALSO ^

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: