【翻译】FreeMarker——入门
1. Template + data-model = output
data-model是一个树状模型,通常是一个java对象。
2.data-model 入门
hashes(散列):目录变量(the root, misc)
scalars(标量):存储单个值的变量(size, price, name and foo)
sequences(序列):They store subvariables like hashes, but here subvariables doesn't have a name, they are just items in a list. (数组?)(animals)
(root)
|
+- animals
| |
| +- (1st)
| | |
| | +- name = "mouse"
| | |
| | +- size = "small"
| | |
| | +- price = 50
| |
| +- (2nd)
| | |
| | +- name = "elephant"
| | |
| | +- size = "large"
| | |
| | +- price = 5000
| |
| +- (3rd)
| |
| +- name = "python"
| |
| +- size = "medium"
| |
| +- price = 4999
|
+- misc
|
+- foo = "Something"
scalars的数据类型:
- String
- Number
- Date-like: Either a date-time (stores a date with time of the day), or a date (no time of day), or a time (time of day, no date).
- Boolean
Summary
- data-model呈现树状结构;
- Scalars store a single value. The value can be a string or a number or a date-time/date/time or a boolean.
- Hashes are containers that store other variables and associate them with a unique lookup name.
- Sequences are containers that store other variables in an ordered sequence. The stored variables can be retrieved via their numerical index, starting from 0.
3. template 入门
3.1 基本语法
- 插值
${...}:FreeMarker将在输出中将其替换为大括号内的表达式的实际值。 - FTL标签(FTL=FreeMarker Template Language,FreeMarker模板语言):类似于HTML标签(but they are instructions to FreeMarker and will not be printed to the output)。标签名以#开头(用户定义的FTL标签使用@替代#)。
- 注释:包含在
<#--和-->之间。不会输出到最终的HTML文件中,因为FreeMarker在编译的时候会跳过这些注释。
除了以上3中语法中的内容都会被视为静态文本,即FreeMarker编译时不会处理这些文本,而是直接输出到HTML文件中。
3.2 常用指令
使用FTL标签可以引用所谓的指令。
3.2.1 if指令
例子:
Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if>!
如果条件为false,<#if condition>和<#if>之间的内容将会被跳过。
配合<#else>指令可以指定条件不成立时执行的内容。
使用<#elseif>进一步细化条件。
3.2.2 list指令
语法:
<#list sequence as loopVariable>repeatThis</#list>
使用示例一:
<ul>
<#list misc.fruits as fruit>
<li>${fruit}
</#list>
</ul>
使用示例二:
<#list misc.fruits>
<ul>
<#items as fruit>
<li>${fruit}
</#items>
</ul>
</#list>
示例一的缺点:如果misc.fruits的length刚好为0,一对空的ul标签仍会输出到最终的HTML文件中。
示例二避免了这个问题。如果 misc.fruits的length为0,list中的所有内容都将被跳过,因此ul标签不会出现在最终的HTML文件中。
如果只是想使用某个符号将sequence 中的各项数据分隔开显示,可以使用<#sep>指令。
示例三:
<#--Template-->
<p>Fruits: <#list misc.fruits as fruit>${fruit}<#sep>, </#list>
<p>Fruits: orange, banana
可是!如果这里的 misc.fruits 的 length 又为0呢??
一个<#list>指令就像一个<#if>指令,可以有一个<#else>指令,当 sequence 的length为0时执行。
示例四:
<p>Fruits: <#list misc.fruits as fruit>${fruit}<#sep>, <#else>None</#list>
事实上,示例四有个更简单的写法:
<p>Fruits: ${fruits?join(", ", "None")}
3.2.3 include指令
使用include指令,您可以将另一个文件的内容插入到模板中。
示例:
<#include "/copyright_footer.html">
3.3 指令的混合使用
各种指令可以混合使用
3.4 使用内置函数(bulit-ins)
所谓的内置函数就像子变量(或者说,类似Java中的方法)不是来自data-model,而是由FreeMarker添加到value。
为了区分一个子变量是来自FreeMarker而不是来自data-model,我们访问这个子变量时需要使用问号(?)来替代点(.)。
以下是一些使用例子:
- user?upper_case:返回全部大写的user值
- animal.name?cap_first:返回首字母大写的animal.name值
- user?length:返回user值(一个字符串)中字符个数
- animals?size:返回animals序列的长度
- 如果你在
<#list animals as animal>和相应的<#list>之间:- animal?index:返回当前animal的index值(从0开始)
- animal?counter:类似于index值,但是下标从1开始
- animal?item_parity:给出字符串“odd”或“even”,这取决于当前的计数器奇偶校验。这通常用于用交替颜色着色行,如:
<td class="${animal?item_parity}Row">
一些内置函数需要更多的参数来指定行为,如:
- animal.protected?string("Y", "N") return the string "Y" or "N" depending on the boolean value of animal.protected.
- animal?item_cycle('lightRow', 'darkRow') is the more generic variant of item_parity from earlier.
- fruits?join(", "): converts the list to a string by concatenating items, and inserting the parameter separator between each items (like "orange, banana")
- user?starts_with("J") gives boolean true of false depending on if user starts with the letter "J" or not.
如果需要反复调用更多内置函数,可以使用链接语法。如:<p>${fruits?join(", ")?cap_first}</p>(fruits=["apple","banana"])的HTML输出为<p>Apple, banana</p>。
3.5 处理缺失的变量
FreeMarker不允许引用缺少的变量,除非你明确指出如果变量丢失了该怎么办。
注意:在FreeMarker中,缺失的变量和值为null的变量是一样的。
两种典型的处理:
示例一:
<h1>Welcome ${user!"visitor"}!</h1>
紧跟在感叹号(!)后面的值为默认值,当且仅当user变量值为null或丢失时,将user变量值显示为"visitor"。
示例二:
<#if user??><h1>Welcome ${user}!</h1></#if>
配合if指令使用,当user变量值为null或丢失时跳过这部分内容。
注意:使用(animals.python.price)!0来替代animals.python.price!0,以防 animals 或 animals.python 的值为null或丢失。前者可以在 animals 或 animals.python 或 animals.python.price 为null或丢失时显示0,而后者只能在animals.python.price为null或丢失时显示0(animals 或 animals.python 为null或丢失时报错“undefined variable”)。同理地,使用(animals.python.price)??替代animals.python.price??。
3.6 转义为HTML,XML和其他标记(特殊符号)
如果做了合适的配置(配置由programmers修改),FreeMarker可以将${...}中的值自动转义。
推荐的做法:使用ftlh文件来激活HTML的自动转义,使用ftlx来激活XML的自动转义。
如果ftl文档目前没有自动转义且不能修改配置,则可以在ftl文档首行加入下面这行代码来实现自动转义:
<#--生成HTML文件-->
<#ftl output_format="HTML">
<#--生成XML文件-->
<#ftl output_format="HTML">
如果要打印的字符串值是故意包含特殊符号,需要这样禁止自动转义:${value?no_esc}
【翻译】FreeMarker——入门的更多相关文章
- Freemarker入门案例
Freemarker入门案例 首先需要到freemarker官方下载freemarker的jar包,导入到项目中,如:freemarker-2.3.19.jar 1.先建个freemarker的工具类 ...
- freemarker入门实例
freemarker入门实例 1.设计思路 (1)新建Maven Project (2)生成freemarker模板 (3)写freemarker页面ftl文件 (4)写测试文件 2.新建Maven ...
- 网页静态化技术--Freemarker入门
网页静态化技术:为什么要使用网页静态化技术 网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类的频道. 对于电商网站的商品详细页来说,至少几百万个商品,每个商品又 ...
- Freemarker入门
Freemarker入门 工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId& ...
- Freemarker入门Demo
1:工程引入依赖 <dependency> <groupId>org.freemarker</groupId> <artifactId>freemark ...
- freeMarker入门示例
1.创建Web项目freeMarkerDemo. 2.添加jar包---freemarker-2.3.9.jar. 3.在WebContent目录下新建templates文件夹,用于放置模板文件ftl ...
- freemarker入门实例与源码研究准备工作
首先去freemarker官网下载源码jar包,本文是基于freemarker-2.3.21.tar.gz进行研究的.解压源码包,找到freemarker的源码部分导入eclipse工程中.需要注意的 ...
- [翻译] Autofac 入门文档
原文链接:http://docs.autofac.org/en/latest/getting-started/index.html 在程序中使用Autofac的基本模式是: 用控制反转(IoC)的思想 ...
- [翻译]lpeg入门教程
原文地址:http://lua-users.org/wiki/LpegTutorial 简单匹配 LPeg是一个用于文本匹配的有力表达方式,比Lua原生的字符串匹配和标准正则表达式更优异.但是,就像其 ...
随机推荐
- 学习一点Markdown的基本知识
本文于2017年3月18日首发于LinkedIn,请参考链接 这个世界的进步是由一些"懒"的人推动的.今天讲的这个Markdown,其实也是因为一批厌倦了HTML的各种标签的语法, ...
- ###Intent的使用(活动中穿梭)
让活动切换有两种方式 显示意图和隐式意图 显示意图:只能在本应用中穿梭: 隐式意图:可以调用其他应用程序的活动,包括系统应用,但是需要配置清单文件 显式Intent 1) 创建一个新的活动 2) 确定 ...
- Bootsrap 的 Carousel
一.简介 Carousel 就是指轮播图,这里 有完整的代码例子.它可以很简单的就构造出来,结构如下: div.carousel.slide[data-ride="carousel" ...
- 日新进用户200W+,解密《龙之谷》手游背后的压测故事
2017年3月,腾讯正式于全平台上线了<龙之谷>手游,次日冲到了App Store畅销排行第二的位置,并维持到了现在.上线当日百度指数超过40万,微信游戏平台数据显示预约数780多万,而据 ...
- 意外发现的大批量导入数据SqlBulkCopy类
因为要做一个号码归属地查询小功能,因为要导入外部(文本文件)的电话归属地数据,使用的是SqlDataAdapter类,数据不多,只四万有多条,表也只有一个,phoneBook表,使用的是DataTab ...
- 一个基于php+mysql的外卖订餐网站(带源码)
订饭组 一个基于php+mysql的外卖订餐网站,包括前端和后台.源码地址 源码演示地址:http://dingfanzu.com 商家后台系统:http://dingfanzu.com/admin ...
- JavaScript对象原型写法区别
体现对象原型分步式写法 //原型分步式写法 //构造函数 function Person(){} //对象原型 Person.prototype.name = 'Avensatr'; Pers ...
- 20155205 2016-2017-2 《Java程序设计》第4周学习总结
20155205 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 private成员会被继承,只不过子类无法直接存取,必须通过父类提供的方法来存取(若 ...
- 从以往子类化跟踪MouseLeave深入讨论VB6的自定义Hook类
一.关于起因 之前发过一篇博文,是关于VB6中跟踪鼠标移出事件的示例(http://www.cnblogs.com/alexywt/p/5891827.html) 随着业务状况的不断发展,提出了更多的 ...
- 2.Maven 使用
1 Maven使用 1.1 编写POM 就像Make的Makefile,Ant的build.xml一样,Maven项目的核心是pom.xml. 首先创建一个名为hello-world的文件夹(本书中各 ...