freemarker

freemarker页面上的使用

1,<#if condition> 和 </#if>   条件判断语句

2,<#list sequence as loopVariable>repeatThis</#list>  list循环遍历语句

<#list misc.fruits>

  <ul>

    <#items as fruit>

      <li>${fruit}

    </#items>

  </ul>

</#list>

在上面的遍历中,当一个fruit都没有的时候,<ul标签也不会存在。

<#list misc.fruits>

  <p>Fruits:

    <ul>

      <#items as fruit>

        <li>${fruit}  <#sep> and</#sep>

       </#items>

    </ul>

<#else>

      <p>We have no fruits.

</#list>

<#sep> 可以将遍历的结果用and字符串连接起来,而最后一个的后面不会有and

<list> <#else> 可以联合起来使用

3. 内建函数

animal.name?cap_first 给出 animal.name 的首字母大写版本(比如 "Mouse" 来替代 "mouse")

user?length 给出 user 值中 字符的数量(对于 "John Doe" 来说就是8)

4. 处理不存在的变量

<h1>Welcome ${user!"visitor"}!</h1>  如果user变量不存的情况下,就显示后面的visitor

<#if user??><h1>Welcome ${user}!</h1></#if>  用判断语句,如果不存在,就会忽略整个问候语句

参考中文在线手册:http://freemarker.foofun.cn/

原文地址:https://www.cnblogs.com/yanliang12138/p/10160745.html