[% PROCESS header title="Some Interesting Links" %]
<p>
Here are some interesting links:
<ul>
[% FOREACH link = weblinks %]
<li><a href="[% link.url %]">[% link.title %]</a></li>
[% END %]
</ul>
</p>
[% PROCESS footer %]
The first thing to note is that template directives are embedded
within [% and %]. You can
change these values, along with several dozen other configuration
options, but we'll stick with the defaults for now.
The directives within those tags are instructions to the template
processor. They can contain references to variables (e.g.,
[% link.url %]) or language
constructs that typically begin with an uppercase word and may have
additional arguments (e.g., [% PROCESS footer %]).
Anything else outside the tags is plain text and is passed through
unaltered.
The dot (.) operator is used
to access data items within data items, and it tries to do the right
thing according to the data type. For example, each item in the list
could be a reference to a hash array, in which case
link.url would be equivalent to the Perl code
$link->{url}, or it could be an object against
which methods can be called, such as $link->url(
). The dotted notation hides the specifics of your backend
code so that you don't have to know or care about
the specifics of the implementation. Thus, you can change your data
from hash arrays to objects at some later date and slot them straight
in without making any changes to the templates.
Let's now go back to our earlier example and see if
we can make sense of it:
<h3>[% users.size %] users currently logged in:</h3>
<ul>
[% FOREACH user = users %]
[%# 'loop' is a reference to the FOREACH iterator -%]
<li>[% loop.count %]/[% loop.size %]:
<a href="[% user.home %]">[% user.name %]</a>
[% IF user.about %]
<p>[% user.about %]</p>
[% END %]
[% INCLUDE userinfo %]
</li>
[% END %]
</ul>
Anything outside a [% ... %]
directive—in this case, various HTML fragments that are
building a list of users currently logged in to our fictional
system—is passed through intact.
The various constructs that we meet inside the directives are: