HTML-YaTmpl
view release on metacpan or search on metacpan
lib/HTML/YaTmpl.pod view on Meta::CPAN
F<thumb.tmpl> could look like:
1 <:cond link>
2 <:case "$link eq 'yes'">
3 <a href="orig/<=name/>.jpg><img src="<=name/>.jpg"></a>
4 </:case>
5 <:case 1>
6 <img src="<=name/>.jpg">
7 </:case>
8 </:cond>
Now the C<< <:cond> >> at line 1 names C<link>. Thus C<$link> can be used
at line 2 in the C<< <:case: >> condition.
=item B<< <:m name .../> or <:m file>...</:m> or >>
=item B<< <:macro name .../> or <:macro file>...</:macro> >>
=item B<< <:defmacro name>...</:defmacro> >>
C<< <:defmacro> >> defines a macro. For example, one can define a macro as
<:defmacro td><td align="center"><=val/></td></:defmacro>
Later on it can be invoked as
<:m td val="..."/> or <:macro td val="..."/>
Our previous example then can be written as
1 <:defmacro td><td align="center"><=val/></td></:defmacro><#
2 /><:defmacro tr><tr><:for x="<:/>"><#
3 /><=x><:m td val="<:/>"/></=x></:for><:macro td>
4 <:set val><:cond>
5 <:case "$v->[1]>150"><b>very expensive</b></:case>
6 <:case "$v->[1]<100"><b>bargain</b></:case>
7 <:case 1>normal prize</:case>
8 </:cond></:set>
9 </:macro></tr> </:defmacro><#
10 /><:for>
11 <:set goods><:
12 [
13 [apple=>'300'],
14 [pear=>'90'],
15 [cherry=>'82'],
16 [plum=>'120'],
17 ]
18 /></:set>
19 <:code><=goods pre="<table>" post="
20 </table>">
21 <:m tr/></=goods></:code>
22 </:for>
Line 1 defines a macro named C<td> that prints one HTML table cell. It uses
the variable C<val>. Lines 2 to 9 define a macro called C<tr> that
implements a table row. It is designed to be used in a substitution scope
as it assigns the current value C<< <:/> >> to a variable C<x> to build
a C<< <:for> >> scope. In line 3 the macro defined in line 1 is used twice,
once as C<< <:m/> >> and once as C<< <:macro> >>. Then in line 21 the C<tr>
macro is invoked and generates all table rows.
Macros are bound to a C<HTML::YaTmpl> instance. That means you can define a
set of macros in one template, evaluate it and then evaluate another template
with the same instance using macros defined in the first template.
=item B<< <:set></:set> >>
is used to make up the parameter list for C<< <:for> >>, C<< <:eval> >>,
C<< <:include> >> and C<< <:macro> >> statements. Outside one of these
scopes or inside a C<< <:code> >> segment C<< <:set> >> can be used to
manipulate the current parameter list.
Thus, the example above can be rewritten as:
1 <:set goods><:
2 [
3 [apple=>'300'],
4 [pear=>'90'],
5 [cherry=>'82'],
6 [plum=>'120'],
7 ]
8 /></:set><=goods pre="<table>" post="
9 </table>">
10 <:m tr/></=goods>
reusing the macros as described above.
=back
=head1 Commenting templates
Writing templates is very similar to writing programs. And as programs should
contain comments to be maintainable so do templates. You can even put POD
sections into a template and generate documentation using POD translators.
When thinking about comments within templates I first thought that using
<:# any comment/>
or
<:># any comment</:>
would be appropriate. It invokes the perl interpreter to evaluate just
# any comment
which is a perl comment and evaluates to C<undef>. Although it works it
can slow down template eveluation. Hence I decided to let comments look like
<# this is a comment />
or
<#> this is a comment </#>
Thus, to put a longer comment (POD section) into your template surround it
with C<< <#> >> and C<< </#> >>:
<#>
=head1 NAME
( run in 0.961 second using v1.01-cache-2.11-cpan-39bf76dae61 )