使用内置函数

本章节将介绍怎样使用 FreeMarker 的内置函数。

FreeMarker 内建函数很像子变量(如果了解 Java 术语的话,也可以说像方法), 内置函数并不是数据模型中的东西,是 FreeMarker 在数值上添加的,不同类型的数值拥有不同的内置函数。例如:数字和字符串上面拥有的内置函数肯定不一样。

如果你了 JavaScript 语言,它们调用函数方式如下:

// 调用 toLowerCase 函数将字符串转为小写,
// 然后使用 substr 函数截取字符串前5个字符“Hello”
var str = "Hello World";
str = str.toLowerCase().substr(0,5);

如果在 FreeMarker 中则不能使用类似 JavaScript 语言的方式去调用内置方法,如下:

<!-- 将字符串转换成大写 -->
<#assign str="Hello World">
${str.upper_case}

上面代码是错误的。因为点号(“.”)在 FreeMarker 中用来获取 hash 数据对象中的数据,例如:${user.name} 或 ${user.address.city}。 

为了将内置函数和子变量(user.name 或 user.address.city),你使用 ?(问号) 代替 .(点) 来访问内置函数。如下:

<!-- 将字符串转换成大写 -->
<#assign str="Hello World">
${str?upper_case}

输出结果:

HELLO WORLD

常用内建函数的示例

  • user?html:对 user 变量进行 HTML 转义, 比如: & 会由 &amp; 来代替。

  • user?upper_case:将 user 变量的值转换成大写,比如:将 “John Doe” 转换成 “JOHN DOE”

  • animal.name?cap_first:将 animal.name 变量的首字母大写,比如:“mouse” 转换成 “Mouse”

  • user?length:计算 user 变量中字符的数量,如:“John Doe” 长度为 8

  • animals?size:计算 animals 序列中项目的个数

如果在 <#list animals as animal> 和对应的 </#list> 标签中:

  • animal?index:获取当前 list 指令迭代数据的索引值,该值从 0 开始

  • animal?counter:和 index 类似,但是该值是从 1 开始

  • animal?item_parity:基于当前计数奇偶性,给出字符串 "odd" 或 "even"。在给不同行着色时非常有用,比如在 <td class="${animal?item_parity}Row">中。

一些内建函数需要参数来指定行为,比如:

  • animal.protected?string("Y", "N"):基于 animal.protected 的布尔值来返回字符串 “Y” 或 “N”。

  • animal?item_cycle('lightRow','darkRow'):是之前介绍的 item_parity 更为常用的变体形式。

  • fruits?join(", "):通过连接所有项,将列表转换为字符串, 在每个项之间插入参数分隔符。比如:“orange,banana”

  • user?starts_with("J"):根据 user 的首字母是否是 "J" 返回布尔值 true 或 false。

内建函数应用可以链式操作,比如:user?upper_case?html 会先转换用户名到大写形式,之后再进行HTML转义。

实例:演示上面的内置函数的用法。

<html>
<head>
    <title>使用内置函数</title>
</head>
<body>

<p>常用内建函数的示例:</p>
<#assign htmlCode="hello <label style=\"color:red;\">world</label>" />
<#assign user="administrator" />
<p>html=${htmlCode?html}</p>
<p>upper_case=${user?upper_case}</p>
<p>cap_first=${user?cap_first}</p>
<p>length=${user?length}</p>

<p>list 标签中内置函数示例:</p>
<#assign books=["Java", "C#", "C++"] />
<p>size=${books?size}</p>
<#list books>
    <#items as book>
        <p>index=${book?index}</p>
        <p>counter=${book?counter}</p>
        <p>item_parity=${book?item_parity}</p>
    </#items>
</#list>

<p>一些内建函数需要参数来指定行为</p>
<#assign colors=["red", "blue", "green"] />
<#assign flag=false />
<p>string=${flag?string("Y", "N")}</p>
<#list books>
    <#items as book>
        <p>item_parity=${book?item_cycle("lightRow", "darkRow")}</p>
    </#items>
</#list>
<p>join=${colors?join(",")}</p>
<#if user?starts_with("a")>
    <p>starts_with=${user} 以a开头</p>
<#else>
    <p>starts_with=${user} 不以a开头</p>
</#if>

</body>
</html>

输出结果:

<html>
<head>
    <title>使用内置函数</title>
</head>
<body>
<p>常用内建函数的示例:</p>
<p>html=hello &lt;label style=&quot;color:red;&quot;&gt;world&lt;/label&gt;</p>
<p>upper_case=ADMINISTRATOR</p>
<p>cap_first=Administrator</p>
<p>length=13</p>

<p>list 标签中内置函数示例:</p>
<p>size=3</p>
        <p>index=0</p>
        <p>counter=1</p>
        <p>item_parity=odd</p>
        <p>index=1</p>
        <p>counter=2</p>
        <p>item_parity=even</p>
        <p>index=2</p>
        <p>counter=3</p>
        <p>item_parity=odd</p>

<p>一些内建函数需要参数来指定行为</p>
<p>string=N</p>
        <p>item_parity=lightRow</p>
        <p>item_parity=darkRow</p>
        <p>item_parity=lightRow</p>
<p>join=red,blue,green</p>
    <p>starts_with=administrator 以a开头</p>

</body>
</html>
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号