App-Dapper
view release on metacpan or search on metacpan
$ export EVAL_PERL=true
Then, you can use `perl` or `rawperl` to include perl code in your templates.
For instance:
[% a = "Apple" %]
[%~ PERL %]
my $a = "[% a %]";
print "The variable \$a is \"$a\"";
$stash->set('b', "Banana");
[% END %]
[% b %]
This would print the following:
The variable $a is "Apple"
Banana
During execution, anything printed to STDOUT will be inserted into the
template. Also, the $stash and $context variables are set and are references
to objects that mimic the interface provided by Template::Context and
Template::Stash. These are provided for compatibility only. $self contains
the current Template::Alloy object.
The `rawperl` block can be used as well. It operates the same as the `perl`
block does except that you will need to append to the $output variable rather
than just calling `print`.
## Includes
An `include` directive parses the contents of a file or `block` and inserts them
into the template. Variables that are defined or modified within the included
bits are discarded after the include occurs. Example:
[% include "path/to/template.html" %]
[% file = "path/to/template.html" %]
[% include $file %]
[% block foo %]This is foo[% end %]
[% include foo %]
Arguments may also be passed to the template:
[% include "path/to/template.html" a = "An arg" b = "Another arg" %]
Multiple filenames can be passed by separating them with a plus, a space, or
commas. Any supplied arguments will be used on all templates. Example:
[% include "path/to/template1.html",
"path/to/template2.html" a = "An arg" b = "Another arg" %]
If it's important that the included file *not* be parsed for directives, an
alternative directive may be used called `insert`. `Insert` directives act in
exactly the same way that `include` directives do, but does not process any
directives that the inserted file might contain. Example:
[% insert "path/to/template1.html",
"path/to/template2.html" %]
## Macros
The `macro` directive can be thought of as a way to define a miniature function
that can be used elsewhere in the template. Example:
[% macro foo(i, j) block %]You passed me [% i %] and [% j %].[% end %]
[% foo(1, 2) %]
Result:
You passed me 1 and 2.
Another example:
[% macro bar(max) foreach i = [1 .. max] %]([% i %])[% end %]
[% bar(4) %]
Result:
(1)(2)(3)(4)
## Plugins
Layout plugins provide a way to get access to special functionality that are
more complicated than what [filters](#filters) provide. This section defines some
of the plugins that are distributed with Dapper. For a comprehensive list, see
[Template::Toolkit plugin](http://template-toolkit.org/docs/manual/Plugins.html).
### Datafile
This plugin provides a simple facility to construct a list of hash references,
each of which represents a data record of known structure from a specified data
file. A filename must be specified. An optional delim parameter may also be
provided to specify an alternate delimiter character.
The format of the file is intentionally simple. The first line defines the field
names, delimited by colons with optional surrounding whitespace. Subsequent lines
then defines records containing data items, also delimited by colons. For example:
id : name
mdb: Mark Benson
lkb: Loomis Buschwa
Each line is read, split into composite fields, and then used to initialize a
hash array containing the field names as relevant keys. The plugin returns a
blessed list reference containing the hash references in the order as defined in
the file. Example:
[% use userlist = datafile('users.txt', delim = ':') %]
[% for user in userlist %]
[% user.id %] -> [% user.name %]
[% end %]
Result:
mdb -> Mark Benson
lkb -> Loomis Buschwa
The first line of the file must contain the field definitions. After the first
line, blank lines will be ignored, along with comment line which start with a '#'.
( run in 2.549 seconds using v1.01-cache-2.11-cpan-2398b32b56e )