The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

App::Followme::Template - Handle templates and prototype files

SYNOPSIS

    use App::Followme::Template;
    my $template = App::Followme::Template->new;
    my $render = $template->compile($template_file);
    my $output = $render->($hash);

DESCRIPTION

This module contains the methods that perform template handling. A Template is a file containing commands and variables for making a web page. First, the template is compiled into a subroutine and then the subroutine is called with a hash as an argument to fill in the variables and produce a web page.

METHODS

This module has one public method:

$sub = $self->compile($template_file);

Compile a template and return the compiled subroutine. A template is a file containing commands and variables that describe how data is to be represented. The method returns a subroutine reference, which when called with a metadata object, returns a web page containing the fields from the metadata substituted into variables in the template. Variables in the template are preceded by Perl sigils, so that a link would look like:

    <li><a href="$url">$title</a></li>

TEMPLATE SYNTAX

Templates support the basic control structures in Perl: "for" loops and "if-else" blocks. Creating output is a two step process. First you generate a subroutine from one or more templates, then you call the subroutine with your data to generate the output.

The template format is line oriented. Commands are enclosed in html comments (<!-- -->). A command may be preceded by white space. If a command is a block command, it is terminated by the word "end" followed by the command name. For example, the "for" command is terminated by an "endfor" command and the "if" command by an "endif" command.

All lines may contain variables. As in Perl, variables are a sigil character ('$' or '@') followed by one or more word characters. For example, $name or @names. To indicate a literal character instead of a variable, precede the sigil with a backslash. When you run the subroutine that this module generates, you pass it a metadata object. The subroutine replaces variables in the template with the value in the field built by the metadata object.

If the first non-white characters on a line are the command start string, the line is interpreted as a command. The command name continues up to the first white space character. The text following the initial span of white space is the command argument. The argument continues up to the command end string.

Variables in the template have the same format as ordinary Perl variables, a string of word characters starting with a sigil character. for example,

    $body @files

are examples of variables. The following commands are supported in templates:

do

The remainder of the line is interpreted as Perl code.

for

Expand the text between the "for" and "endfor" commands several times. The argument to the "for" command should be an expression evaluating to a list. The code will expand the text in the for block once for each element in the list.

    <ul>
    <!-- for @files -->
    <li><a href="$url">$title</a></li>
    <!-- endfor -->
    </ul>
if

The text until the matching endif is included only if the expression in the "if" command is true. If false, the text is skipped.

    <div class="column">
    <!-- for @files -->
    <!-- if $count % 20 == 0 -->
    </div>
    <div class="column">
    <!-- endif -->
    $title<br />
    <!-- endfor -->
    </div>
else

The "if" and "for" commands can contain an else. The text before the "else" is included if the expression in the enclosing command is true and the text after the "else" is included if the "if" command is false or the "for" command does not execute. You can also place an "elsif" command inside a block, which includes the following text if its expression is true.

ERRORS

What to check when this module throws an error

Couldn't read template

The template is in a file and the file could not be opened. Check the filename and permissions on the file. Relative filenames can cause problems and the web server is probably running another account than yours.

Unknown command

Either a command was spelled incorrectly or a line that is not a command begins with the command start string.

Missing end

The template contains a command for the start of a block, but not the command for the end of the block. For example an "if" command is missing an "endif" command.

Mismatched block end

The parser found a different end command than the begin command for the block it was parsing. Either an end command is missing, or block commands are nested incorrectly.

Syntax error

The expression used in a command is not valid Perl.

LICENSE

Copyright (C) Bernie Simon.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

AUTHOR

Bernie Simon <bernie.simon@gmail.com>