NAME
Text::Template::Simple - A simple template class mainly for web applications.
SYNOPSIS
use Text::Template::Simple;
my $template = Text::Template::Simple->new(
globals => {
foo => 'bar' , # define $foo
bar => ['baz'], # define @bar
blah => { # define %blah
key => 'value',
},
},
);
my $result = $template->compile(
'Hello <%$foo%>. Key is: <%$blah{key}%> and Your name is <%$name%>.',
{
name => 'Burak',
});
print $result;
DESCRIPTION
This module provides a simple and high level access to Text::Template
interface. It also adds a PREPEND
parameter to enable strict
mode and define the variables before using them. If your perl version is smaller than 5.6
; the vars
pragma, and if it is greater; our
function will be used to define variables in the template. Main purpose of this module is to setup that variable definiton code and initialize all variables if any of them has the value undef
.
METHODS
new
The object constructor. Takes several parameters:
delimiters
The default delimiter set is: <% %>
. You can pass your own delimiters as an arrayref:
Text::Template::Simple->new(
delimiters => ['{', '}'],
)
global
You can define variables that are globally accessible by all templates.
Example:
$globals = {
foo => 'bar' , # define $foo
bar => ['baz'], # define @bar
blah => { # define %blah
key => 'value',
},
};
Text::Template::Simple->new(
globals => $globals,
)
dummy
This must be a dummy package/class name. The module will compile the template into this class. If you don't set it, the default class Text::Template::Simple::Dummy
will be used.
extend
If this has a true value, you can use loop constructs in templates, instead of modifying the $OUT
variable directly. However, there are some caveats. Here is a sample template:
<% foreach (qw[Perl SomeOtherLang]) { %>
<% if ($_ =~ /perl/i) {%>
$_ rules!\n
<% } else { %>
$_ sux!\n
<% } %>
<%}%>
Which is nearly (because the whitespaces around the string is also included in the result of above template) equal to this regular template code:
<%
foreach (qw[Perl SomeOtherLang]) {
if ($_ =~ /perl/i) {
$OUT .= qq~$_ rules!\n~;
} else {
$OUT .= qq~$_ sux!\n~;
}
}
%>
When you use the extended interface, be aware that you can not use templates inside the constructs. They are basically perl strings (i.e. they are not static template texts). So, you can use symbols like \n
or \r
or any variable directly, also any whitespace you put before or after the string will be printed too:
<% if ($_ =~ /perl/i) {%>
$_ rules!\n
<% } %>
If there are some curlies that aren't balanced while parsing the template, you'll get this warning:
LOOP ERROR: %d unmatched curlies!
%d can be positive or negative. Negative value indicates that the parser closed an un-opened loop (i.e. you have an extra right curly or your loop start misses a left curly).
extend
is disabled by default and behaviour is considered experimental.
Why extend?
Because I'm tired of writing this all the time:
$OUT .= qq~blah blah blah~;
this one seems more simple:
blah blah blah
debug
If set to a true value, interface will warn
you about the template compiling process. Currently, it'll not give you any useful information unless you want to patch Text::Template::Simple
.
Debugging is disabled by default.
compile TEMPLATE, PARAMS
Compiles the template into its final form. Takes two parameters. First parameter is the raw template code as a string and second is the parameters you can set. PARAMS
must be a HASHREF
.
my $result = $template->compile('Your name is {$name}.',
{
name => 'Burak', # set $name
});
BUGS
Complete interface of
Text::Template
is not supportedParameter names can not start with minus
Example:
-dummy
or-Dummy
is unvalid. Usedummy
orDummy
orDUMMY
etc...Only single string data is accepted as a template text
Filehandles and any other things are not supported. You must supply a single text parameter as the template code.
SAFE
,BROKEN_ARG
and friends are not supportedMany parameters of
Text::Template
are not supported.Extended interface has some issues
When using
extend
ed interface, you have to use this style:<% if (condition) { %> foo <% } elsif (condition) { %> bar <% } else { %> baz <% } %>
If you use curlies separately, you'll probably broke the code and get errors:
<% if (condition) { %> foo <% } %> <% elsif (condition) { %> bar <% } %> <% else { %> baz <% } %>
This is not a good style anyway (at least in my opinion). The other thing you must not do is putting comments or other things above loops:
<% # blah blah blah if (1) { %> blah <% } %>
or
<% my $blah = 1; if ($blah) { %> blah <% } %>
They'll break the current implementation. You can replace the
if
s above withwhile
,for
andforeach
. The same rules apply to all of them.
There may be many more... Please report if you find any bugs.
SEE ALSO
AUTHOR
Burak Gürsoy, <burak@cpan.org>
COPYRIGHT
Copyright 2004-2005 Burak Gürsoy. All rights reserved.
LICENSE
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 566:
Non-ASCII character seen before =encoding in 'Gürsoy,'. Assuming CP1252