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>

提供了一个基于51单片机的RFID门禁系统的完整资源文件,包括PCB图、原理图、论文以及源程序。该系统设计由单片机、RFID-RC522频射卡模块、LCD显示、灯控电路、蜂鸣器报警电路、存储模块和按键组成。系统支持通过密码和刷卡两种方式进行门禁控制,灯亮表示开门成功,蜂鸣器响表示开门失败。 资源内容 PCB图:包含系统的PCB设计图,方便用户进行硬件电路的制作和调试。 原理图:详细展示了系统的电路连接和模块布局,帮助用户理解系统的工作原理。 论文:提供了系统的详细设计思路、实现方法以及测试结果,适合学习和研究使用。 源程序:包含系统的全部源代码,用户可以根据需要进行修改和优化。 系统功能 刷卡开门:用户可以通过刷RFID卡进行门禁控制,系统会自动识别卡片并判断是否允许开门。 密码开门:用户可以通过输入预设密码进行门禁控制,系统会验证密码的正确性。 状态显示:系统通过LCD显示屏显示当前状态,如刷卡成功、密码错误等。 灯光提示:灯亮表示开门成功,灯灭表示开门失败或未操作。 蜂鸣器报警:当刷卡或密码输入错误时,蜂鸣器会发出报警声,提示用户操作失败。 适用人群 电子工程、自动化等相关专业的学生和研究人员。 对单片机和RFID技术感兴趣的爱好者。 需要开发类似门禁系统的工程师和开发者。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值