template一览


Template:最简单的 template 是一个html文件(甚至可以是一个txt文本文件,freemarker并没有做明确规定)

interpolation: ${...} 在输出的文件中会将实际的值代替括号中的变量

FTL tags : FTL(Freemarker Template Language) . 标签开始 # 

Comments(注释):<!#-- -->

 

 

Freemarker 只会将 interpolation 和 ftl tags标签进行解析,其他的html内容会按照原样输出。

 

 

 

最常用的 directives(指令):

if directives:

简单使用:

Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if>

级联属性的判断:

<#if animals.python.price == 0>
  Pythons are free today!
</#if>

不相等:

<#if animals.python.price != 0>
  Pythons are not free today!
</#if>

小于:

<#if animals.python.price < animals.elephant.price>
  Pythons are cheaper than elephants today.
</#if>

<#else>:

 

<#if animals.python.price < animals.elephant.price> Pythons are cheaper than elephants today. <#else> Pythons are not cheaper than elephants today. </#if>

<#elseif>:

<#if animals.python.price < animals.elephant.price>
  Pythons are cheaper than elephants today.
<#elseif animals.elephant.price < animals.python.price>
  Elephants are cheaper than pythons today.
<#else>
  Elephants and pythons cost the same today.
</#if>

如果值为boolean类型的值,则不需要判断,如 animals.python.protected 值为true 或 false

<#if animals.python.protected>
  Pythons are protected animals!
</#if>

list directives: 主要针对 sequences.

简单使用:<#list sequence as loopVariable>repeatThis</#list>.其中repeatThis 是遍历sequence的当前项。

<#list animals as animal>
    <tr><td>${animal.name}<td>${animal.price} Euros
 </#list>

使用问题1:

<ul>
<#list misc.fruits as fruit>
  <li>${fruit}
</#list>
</ul>

如果 fruits 长度为0, 它还是会打印一个空的 ul 标签。

替代的写法:

<#list misc.fruits>
  <ul>
    <#items as fruit>
      <li>${fruit}
    </#items>
  </ul>
</#list>

使用分隔符<#sep> :

Template <p>Fruits: <#list misc.fruits as fruit>${fruit}<#sep>, </#list>
Output
<p>Fruits: orange, banana

也可以这样写:

<p>Fruits: <#list misc.fruits as fruit>${fruit}<#sep>, </#sep></#list>

<#list> 和 <#else>使用:

 

<p>Fruits: <#list misc.fruits as fruit>${fruit}<#sep>, <#else>None</#list>

还可以使用最简单的写法:

<p>Fruits: ${fruits?join(", ", "None")}

所有的 list , items, sep , else 都可以在一起使用:

<#list misc.fruits>
  <p>Fruits:
  <ul>
    <#items as fruit>
      <li>${fruit}<#sep> and</#sep>
    </#items>
  </ul>
<#else>
  <p>We have no fruits.
</#list>

include directives:

 

<#include "/copyright_footer.html">

标签可以嵌套使用:

<#list animals as animal>
      <div<#if animal.protected> class="protected"</#if>>
        ${animal.name} for ${animal.price} Euros
      </div>
</#list>

使用freemarker内建的方法:

user?upper_case : 将 user的值转换为大写(like "JOHN DOE" instead of "John Doe"

 

animal.name?cap_first:将值的第一个字母转换为大写字母(like "Mouse" instead of "mouse"

user?length:获取user值的长度

 

animals?size:获取animals的size

可以在<#list animals as animal></#list>标签中使用如下内建方法:

animal?index:返回animal在animals中的索引,索引从0开始。

animal?counter:返回animal 在animals中的位置,从1开始。

animal?item_parity:返回"odd" or "even"。依赖于当前的counter。常用来做隔行变色。

有些内建方法需要指定参数:

animal.protected?string("y", "n"),根据animal.prottected 返回的boolean值,来返回值 “y”, "n"。

animal?item_cycle("lightRow", "darkRow")。

fruits?join(","):将list遍历的每个结果使用分隔符进行分割。

user?starts_with("J"):判断user返回的值是否以"J"开头。

内建方法可以链式使用:如:fruits?join(",")?upper_case,会先将结果进行分割,然后转换成大写。可以使用“.”的方式去调用。

 

处理没有获取的值的数据:

使用默认值:!"value"

 

<h1>Welcome ${user!"visitor"}!</h1>

如果user值为null的话,则赋值为默认值"visitor"。

可以在 name后添加 ?? ,来判断值是否为空。经常和if指令一起使用,如果为false则会跳过当前if内容。

 

<#if user??><h1>Welcome ${user}!</h1></#if>

判断级联的多个值:

如animals.python.price != 0 正确的前提是,animals.python不为空,animals不为空。所以正确的写法是:

(animals.python.price)!=0。也可以写成(animals.python.price)??

不对特殊值进行转换:

如: name 值为:"Someone&Co.",而${name}输出的值为:"Someone&amp;Co."。

freemarker 会自动转换所有的值。如果不想进行转换,可以这样使用:${value?no_esc}。

需要注意的是此种写法使用是从FreeMarker2.3.24开始使用的。之前的版本使用过期的escape指令来替代。

转载于:https://my.oschina.net/javaGeeker/blog/664591

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值