FreeMaker的基本使用

https://freemarker.apache.org/docs/dgui_quickstart.html

  • text 文本

  • ${...}: 插值,括号内表达式值代表输出 interpolations,形如:  ${ and } (或者 #{ and })

  • FTL tags ( FreeMarker Template Language tags): FreeMarke模板语言标记,类似HTML标记,但是运用于freemaker,不会输出在网页页面。标记以 #/@开始。(用户定义FTL tags 使用 @ ),

  • Comments: 注释, <#-- and -->.网页源码中不可见,因为freemaker跳过他们了.

除了FTL tag,interpolation和注释,其他内容都视为静态文本。在网页直接输出。

 

 

数据类型&基本表达

freemaker的基本语法

<# ... > 中存放所有freemaker的内容,之外的内容全部原样输出。
<@ ... /> 是函数调用

方法与函数的区别:方法来源于数据模型(Java对象的方法), 函数是模板中定义的,二者使用方式是相同的。

自定义函数:

无参的

 

带参数的

 

声明变量

数据类型 

两个定界符内的内容中,第一个符号表示指令或者函数名,其后的跟随参数。freemaker提供的控制包括如下:
<#if condition><#elseif condition><#else></#if> 条件判断
<#list hash_or_seq as var></#list> 遍历hash表或者collection(freemaker称作sequence)的成员
<#macro name param1 param2 ... ><#nested param></#macro> 宏,无返回参数
<#function name param1 param2><#return val></#function>函数,有返回参数
var?member_function(...) 用函数对var进行转换,freemaker称为build-ins。实际内部实现类似member_function(var, ...)
stringA[M .. N] 取子字符串,类似substring(stringA, M, N)
{key:value, key2:value2 ...} 直接定义一个hash表
[item0, item1, item2 ...] 直接定义一个序列
hash0[key0] 存取hash表中key对应的元素
seq0[5] 存取序列指定下标的元素
<@function1 param0 param1 ... /> 调用函数function1
<@macro0 param0 param1 ; nest_param0 nest_param1 ...> nest_body </@macro> 调用宏,并处理宏的嵌套
<#assign var = value > 定义变量并初始化
<#local var = value> 在 macro 或者 function 中定义局部变量并初始化
<#global var = value > 定义全局变量并初始化
${var} 输出并替换为表达式的值
<#visit xmlnode> 调用macro匹配xmlnode本身及其子节点
<#recurse xmlnode> 调用macro匹配xmlnode的子节点

指令:derectives,FTL tag形式

if

if语句 选择性跳转。举例,对不同的人不一样的打招呼。 


<html>

<head>

<title>Welcome!</title>

</head>

<body>

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

</h1>

<p>Our latest product: <a href="${latestProduct.url}">${latestProduct.name}</a>!

</body>

</html>


 

当user字符串等于"Big Joe"时,打招呼为:“ our beloved leader" .

如果条件语句返回值为false,则跳过。.

 

若price值为0,打印输出为  "Pythons are free today!" 

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

标签<#else><#elseif>使用 逻辑结构与java相同。举例:

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

 

<#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>

If标签内直接使用一个有true/false返回值的条件语句,也可以直接使用

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

list 

列表标签 用于列举

<p>We have these animals:
<table border=1>
  <#list animals as animal>
    <tr><td>${animal.name}<td>${animal.price} Euros
  </#list>
</table>

输出为:所有动物:老鼠,大象,蟒蛇

<p>We have these animals:
<table border=1>
    <tr><td>mouse<td>50 Euros
    <tr><td>elephant<td>5000 Euros
    <tr><td>python<td>4999 Euros
</table>

以上表格输出,转化为list

<#list sequence as loopVariable>

repeatThis

</#list>.

每当有sequence,repeatThis 部分重复输出,sequence可以为任何表达式.

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

 

上面例子的问题:当fruit为0时,仍会输出空<ul></ul>.下面的方法可以避免此类情况的发生。

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

item存在时输出,否则就跳过,不做任何操作

另一种常见的方式,可以实现输出内容的空格控制。

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

 0个 fruits。只打印"Fruits:" .list可以像if语句一样执行else, 当fruit为0时,执行:

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

Note:

As a matter of fact, this simplistic example could be written like this, but it uses language devices that are off topic here:

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

All these directives (list, items, sep, else) can be used together:

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

 

include 

插入其他文件的内容到当前模板中。

假设你要在多个也main显示相同的copyright.可以单独创建copyrigh文件,在其他地方插入该文件。首先,保存文件为:copyright_footer.html:

<hr>
<i>
Copyright (c) 2000 <a href="http://www.acmee.com">Acmee Inc</a>,
<br>
All Rights Reserved.
</i>

 

当你需要使用该文件时:
<html>
<head>
  <title>Test page</title>
</head>
<body>
  <h1>Test page</h1>
  <p>Blah blah...
  <#include "/copyright_footer.html">
</body>
</html>

 

同时使用多个

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

 

处理缺失变量

变量不存在或者为空,指定默认值 ${vira_name!default_value}

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

使用双问号判断缺失值??

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

 转义HTML,XML和其他标记

模板生成HTML. 建议的做法是使用ftlh文件扩展名来激活HTML自动转义,使用ftlx文件扩展名来激活XML自动转换。你可以尝试自动转义,如$ {“<”},然后检查原始输出(对于 HTML或XML转义)。 如果不是,并且配置不会被调整,请将其添加为模板的第一行:

<#ftl output_format="HTML">

插值 

形如:${expression},expression 可以是任何形式(e.g. ${100 + x}).

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值