Template

 1  頁面元素

The dot operator '.' is used to access into lists and hashes or to call object methods. The FOREACH directive is provided for iterating through lists, and various logical tests are available using directives such as IF, UNLESS,

 

[% FOREACH section = menu %]
   <a href="[% root %]/[% section %]/index.html">[% section %]</a>
[% END %]

<b>Client</a>: [% client.name %] (id: [% client.id %])

[% IF shopcart.nitems %]
   Your shopping cart contains the following items:
   <ul>
   [% FOREACH item = shopcart.contents %]
     <li>[% item.name %] : [% item.qty %] @ [% item.price %]
   [% END %]
   </ul>

   [% checkout(shopcart.total) %]

[% ELSE %]
   No items currently in shopping cart.
[% END %]
 2  [% mode == 'graphics' ? "Graphics Mode Enabled" : "Text Mode" %]
[% var = 'value' IF some_condition %]

 3  取值不需要加符號,直接引用變量名,字符串連接用‘_’
[% copyright = '(C) Copyright' _ year _ ' ' _ author %]
等同於
[% copyright = "(C) Copyright $year $author" %]
 4  默認值
The DEFAULT directive is similar to SET but only updates variables that are currently undefined or have no "true" value (in the Perl sense). 
[% DEFAULT 
   title = 'Hello World'
   bgcol = '#ffffff'
%]
<html>
  <head>
    <title>[% title %]</title>
  </head>
  <body bgcolor="[% bgcol %]">
    ...etc...
 5  條件處理
 5.1  IF / UNLESS / ELSIF / ELSE
[% IF age < 10 %]
   Hello [% name %], does your mother know you're 
   using her AOL account?
[% ELSIF age < 18 %]
   Sorry, you're not old enough to enter 
   (and too dumb to lie about your age)
[% ELSE %]
   Welcome [% name %].
[% END %]
== != < <= > >= && || ! and or not

 5.2  SWITCH / CASE
[% SWITCH myvar %]
[%   CASE 'value1' %]
       ...
[%   CASE ['value2', 'value3'] %]   # multiple values
       ...
[%   CASE myhash.keys %]            # ditto
       ...
[%   CASE %]                        # default
       ...
[% END %]

 5.3  FOREACH
[% foo   = 'Foo'
   items = [ 'one', 'two', 'three' ]
%]

Things:
[% FOREACH thing IN [ foo 'Bar' "$foo Baz" ] %]
   * [% thing %]
[% END %]

Items:
[% FOREACH i IN items %]
   * [% i %]
[% END %]

Stuff:
[% stuff = [ foo "$foo Bar" ] %]
[% FOREACH s IN stuff %]
   * [% s %]
[% END %]
You can use also use = instead of IN if you prefer. [% FOREACH i = items %]
[% userlist = [
    { id => 'tom',   name => 'Thomas'  },
    { id => 'dick',  name => 'Richard'  },
    { id => 'larry', name => 'Lawrence' },
   ]
%]

[% FOREACH user IN userlist %]
   [% user.id %] [% user.name %]
[% END %]
short form: 
[% FOREACH userlist %]
   [% id %] [% name %]
[% END %]

[% users = {
     tom   => 'Thomas',
     dick  => 'Richard',
     larry => 'Lawrence',
   }
%]
[% FOREACH u IN users %]
   * [% u.key %] : [% u.value %]
[% END %]

 5.4  NEXT: The NEXT directive starts the next iteration in the FOREACH loop. 
[% FOREACH user IN userlist %]
   [% NEXT IF user.isguest %]
   Name: [% user.name %]    Email: [% user.email %]
[% END %]

LAST: The LAST directive can be used to prematurely exit the loop. BREAK is also provided as an alias for LAST. 
[% FOREACH match IN results.nsort('score').reverse %]
   [% LAST IF match.score < 50 %]
   [% match.score %] : [% match.url %]
[% END %]

 5.5  The FOREACH directive is implemented using the Template::Iterator module. A reference to the iterator object for a FOREACH directive is implicitly available in the loop variable. The following methods can be called on the loop iterator. 
size()      number of elements in the list
max()       index number of last element (size - 1)
index()     index of current iteration from 0 to max()
count()     iteration counter from 1 to size() (i.e. index() + 1)
first()     true if the current iteration is the first
last()      true if the current iteration is the last
prev()      return the previous item in the list
next()      return the next item in the list

[% FOREACH item IN [ 'foo', 'bar', 'baz' ] -%]
   [%- "<ul>\n" IF loop.first %]
   <li>[% loop.count %]/[% loop.size %]: [% item %]
   [%- "</ul>\n" IF loop.last %]
[% END %]
[% FOREACH group IN grouplist;
     # loop => group iterator
     "Groups:\n" IF loop.first;
     FOREACH user IN group.userlist;
        # loop => user iterator
        "$loop.count: $user.name\n";
     END;
     # loop => group iterator
     "End of Groups\n" IF loop.last;
   END 
%]

 5.6  WHILE
The NEXT directive can be used to start the next iteration of a WHILE loop and BREAK can be used to exit the loop, both as per FOREACH. 

[% WHILE total < 100 %]
   ...
   [% total = calculate_new_total %]
[% END %]
[% WHILE (user = get_next_user_record) %]
   [% user.name %]
[% END %]

 6  FILTER: described in Template::Manual::Config
 6.1  The html filter, for example, escapes the '<', '>' and '&' characters to prevent them from being interpreted as HTML tags or entity reference markers. 
The html filter is an example of a static filter, implemented as: 
sub html_filter {
    my $text = shift;
    for ($text) {
        s/&/&amp;/g;
        s/</&lt;/g;
        s/>/&gt;/g;
    }
    return $text;
}
 6.2  Dynamic filters can accept arguments which are specified when the filter is called from a template. The repeat filter is such an example, accepting a numerical argument which specifies the number of times that the input text should be repeated. 
[% FILTER repeat(3) %]blah [% END %]
output: 
blah blah blah
The repeat filter factory is implemented like this: 
sub repeat_filter_factory {
    my ($context, $iter) = @_;
    $iter = 1 unless defined $iter;

    return sub {
        my $text = shift;
        $text = '' unless defined $text;
        return join('\n', $text) x $iter;
    }
}
 7  META
it's not possible to interpolate other variables values into META variables.
The template variable contains a reference to the main template being processed. These metadata items may be retrieved as attributes of the template. 

[% META
   title   = 'The Cat in the Hat'
   author  = 'Dr. Seuss'
   version = 1.23 
%]
<h1>[% template.title %]</h1>
<h2>[% template.author %]</h2>


發佈了32 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章