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/&/&/g;
s/</</g;
s/>/>/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>
分享到:
相关推荐
Description: Among the many different approaches to "templating" with Perl--such as Embperl, Mason, HTML::Template, and hundreds of other lesser known systems--the Template Toolkit is widely ...
而lua-resty-template和大多数模板引擎是类似的,大体内容有: 模板位置:从哪里查找模板; 变量输出/转义:变量值输出; 代码片段:执行代码片段,完成如if/else、for等复杂逻辑,调用对象函数/方法; 注释:解释...
也许有的中级前端知道type=text/template标签,所以在这里我就不解释他的用处了,你能找到我这里来,说明你已经遇到这个问题了. 我只说如何解决此问题. HTML页面不认<script type="text/template">标签,怎么办? 比如 ...
vue-admin-template前端页面开发框架,你可以把 vue-element-admin当做工具箱或者集成方案仓库,在 vue-admin-template 的基础上进行二次开发,想要什么功能或者组件就去 vue-element-admin 那里复制过来。...
SpringBoot实现vue-admin-template登录接口 vue-admin-template vue-admin-template是一个简化版的vue-element-admin的模板, 适合vue用来做项目的搭建,虽然官方文档以及花裤衩前端大佬 在博客中已经将大部分的疑难...
template-web.js框架
function safety template : item definition; HARA ; Safety plan.etc.
IEEE Transactions Latex Template
java 利用 sping-data-hadoop HbaseTemplate 操作hbase find get execute 等方法 可以直接运行
这是我的博客《前端常用插件之art-template模板引擎》文章的示例代码 具体使用可参考文章,或者网上搜索也可以
mycat2 install-template(mycat2-install-template-1.20.zip)
所有010editor template全集,包括jpg,zip,rar,pdf,swf,flv,exe,等等。非常全,大家快来下载啊
mycat2 install-template(mycat2-install-template-1.21.zip)
artTemplate模板 js替换使用 ### 编写模板 使用一个``type="text/html"``的``script``标签存放模板: <h1>{{title}} {{each list as value i}} 索引 {{i + 1}} :{{value}} {{/each}} ### 渲染...
template Dell iDrac SNMPV2.xml template Dell iDrac SNMPV2.xml
art-template 的 template-web.js 文件,如果官网不能下载,就下载这个,拷贝就可使用
template相关工程模板,在使用之前可以参考学习
使用引用的template.js方式的demo。包含art-template 常用功能,普通展示,if判断,each循环,嵌套循环,管道数据转换方法,原样展示html片段设置。
Template Name: Metronic - Responsive Admin Dashboard Template build with Twitter Bootstrap 3.3.5 Version: 4.5.2 Metronic 是一套精美的响应式后台管理模板,基于强大的 Twitter Bootstrap 框架实现。Metronic...
vue3使用print-template生成pdfDemo