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>
分享到:
相关推荐
本文主要围绕`art-template`这一前端模板引擎,通过`art-template-test`示例代码来深入探讨其核心概念、使用方法以及在实际项目中的应用。 `art-template`是一个轻量级、高性能的JavaScript模板引擎,它旨在解决...
1. **HbaseTemplate的初始化**:在使用HbaseTemplate之前,我们需要在Spring配置文件中配置HBase的相关连接信息,如Zookeeper地址、HBase表名等,并实例化HbaseTemplate。这通常通过@Autowired注解和@Configuration...
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 ...
**C# 模板引擎 TemplateEngine 源码生成** 在.NET开发环境中,C#语言以其强大而灵活的特性被广泛使用。对于许多自动化任务,如网页制作、电子邮件模板生成、XML代码生成以及源代码自动生成,开发者经常需要一种工具...
MongoTemplate是Spring Data MongoDB框架中的一个核心组件,用于在Java应用中方便地操作MongoDB数据库。这个资源包“mongoTemplate工具类Dao层方法封装.zip”显然提供了两种版本的MongoTemplate操作集合的代码示例,...
cd vue-h5-template yarn install yarn dev 目录 √ vite √ 配置多环境变量 √ viewport 适配方案 √ 多 UI 组件库供选择 √ Pinia 状态管理 √ vue-router 4 √ axios 封装及接口管理 √ vite.config.ts 基础...
"MongoDB 使用 MongoTemplate 实现统计和分组" MongoDB 是一个非常流行的 NoSQL 数据库,它可以存储大量的数据,但是有时候我们需要对这些数据进行分析和利用。在本文中,我们将介绍如何使用 MongoTemplate 实现...
而lua-resty-template和大多数模板引擎是类似的,大体内容有: 模板位置:从哪里查找模板; 变量输出/转义:变量值输出; 代码片段:执行代码片段,完成如if/else、for等复杂逻辑,调用对象函数/方法; 注释:解释...
Elsevier-template.doc 文档模板详解 Elsevier-template.doc 是 Elsevier 出版社提供的一种双栏 Word 模板,用于撰写期刊论文。下面将对该模板的标题、描述、标签和部分内容进行详细解释。 标题:Elsevier-...
在本文中,我们将深入探讨如何使用`template-web.js`模板库实现一个动态的二级联动`select`效果。这个实例提供了一个完整的解决方案,适用于那些希望通过JavaScript和模板引擎优化前端交互体验的开发者。 `template...
也许有的中级前端知道type=text/template标签,所以在这里我就不解释他的用处了,你能找到我这里来,说明你已经遇到这个问题了. 我只说如何解决此问题. HTML页面不认<script type="text/template">标签,怎么办? 比如 ...
在本篇中,我们将深入探讨`art-template`的一些常用功能,包括普通展示、if判断、each循环、嵌套循环以及管道数据转换方法,并通过一个`template.js`的示例来理解这些概念。 首先,**普通展示**是最基础的功能,它...
artTemplate模板 js替换使用 ### 编写模板 使用一个``type="text/html"``的``script``标签存放模板: <h1>{{title}} {{each list as value i}} 索引 {{i + 1}} :{{value}} {{/each}} ### 渲染...
MongoDBTemplate mongoTemplate = new MongoDBTemplate(mongoDbFactory); ``` 在`applicationContext.xml`或类似的配置文件中,你会设置MongoDB的相关连接信息,如服务器地址、端口、数据库名和认证信息等。例如: ...
在这个"vue3使用print-template生成pdfDemo"项目中,开发者利用Vue3的特性构建了一个示例,展示了如何将网页内容转化为PDF文档。这个过程通常涉及到前端渲染和第三方库的使用。 首先,我们看到`index.html`是项目的...
"vue-admin-template-master-3.8.0.zip"这个压缩包文件包含了Vue-admin-template的3.8.0版本源代码。Vue-admin-template是一个强大的、高度可定制化的后台管理系统模板,它利用了Vue.js的特性,结合了Element UI组件...
var render = template.compile(source); var html = render({ list: ['摄影', '电影', '民谣', '旅行', '吉他'] }); document.getElementById('content')[removed] = html; [removed]
vue-admin-template前端页面开发框架,你可以把 vue-element-admin当做工具箱或者集成方案仓库,在 vue-admin-template 的基础上进行二次开发,想要什么功能或者组件就去 vue-element-admin 那里复制过来。...
【标题】"mycat2 install-template(mycat2-install-template-1.20.zip)" 指的是MyCat 2的一个安装模板文件,版本号为1.20,存储在一个名为“mycat2-install-template-1.20.zip”的压缩包内。MyCat是开源的、基于...
这里我们关注的是"dropload.js"和"template-web.js"这两个JavaScript文件,它们通常用于网页的下拉刷新功能和网页模板构建。 首先,让我们详细了解一下"dropload.js"。Dropload是一款轻量级的JavaScript插件,专门...