基本操作

条件判断 = != > <

<#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 with boolean condition

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

check variable exist or not ??

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

循环

<#list sequence as loopVariable>repeatThis</#list>

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

这一段代码有个问题就是如果list是空的,也会打印出 <ul></ul>, 可以修改为以下:

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

以分隔符分隔列表的数据,好处是:最后一个数据不会带上分隔符

<p>Fruits: <#list misc.fruits as fruit>${fruit}<#sep>, </#list>
<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>

日期

基本参数:

date: 只显示日期,不显示时间.
如${createTime?date} 或${createTime?date('yyyy-MM-dd')}
time: 只显示时间,不显示日期
如${createTime?time} 或${createTime?time('hh:mm:ss')}
datetime: 时间和日期同时显示
如${createTime} 或${createTime?datetime('yyyy-MM-dd hh:mm:ss')}或${createTime?string('yyyy-MM-dd hh:mm:ss')}

Freemarker预置了一些日期格式

${createTime?string.short}  01:45 PM
${createTime?string.medium}  01:45:09 PM
${createTime?string.long}  01:45:09 PM PST
${createTime?string.full}  01:45:09 PM PST
${createTime?string.xs}  13:45:09-08:00
${createTime?string.iso}  13:45:09-08:00

字符串类型:

日期格式:${book.date?string('yyyy-MM-dd')} 

Map

value is string

<#list testMap?keys as testKey> 
	< option value="${testKey}" >
		${testMap[testKey]}
	</option> 
</#list>

value is not string

<#list testMap?keys as testKey> 
	< option value="${testKey}" >
		${testMap.get(testKey)}
	</option> 
</#list>

include

<#include "/copyright_footer.html">

其它

默认值

Welcome ${user!"visitor"}!

替换 HTML, XML

<#ftl output_format="HTML">

(Use "XML" instead of "HTML" above if you generate XML.)

注释

<#-- comments -->