HTML-YaTmpl
view release on metacpan or search on metacpan
to create a thumbnail without a link.
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 "<:cond>" at line 1 names "link". Thus $link can be used at
line 2 in the "<:case:" condition.
<:m name .../> or <:m file>...</:m> or =item <:macro name .../> or
<:macro file>...</:macro> =item <:defmacro name>...</:defmacro>
"<: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 "td" that prints one HTML table cell. It
uses the variable "val". Lines 2 to 9 define a macro called "tr" that
implements a table row. It is designed to be used in a substitution
scope as it assigns the current value "<:/>" to a variable "x" to
build a "<:for>" scope. In line 3 the macro defined in line 1 is used
twice, once as "<:m/>" and once as "<:macro>". Then in line 21 the
"tr" macro is invoked and generates all table rows.
Macros are bound to a "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.
<:set></:set>
is used to make up the parameter list for "<:for>", "<:eval>",
"<:include>" and "<:macro>" statements. Outside one of these scopes or
inside a "<:code>" segment "<: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.
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 "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 "<#>" and "</#>":
<#>
=head1 NAME
( run in 0.642 second using v1.01-cache-2.11-cpan-39bf76dae61 )