freemarker的在线手册
http://freemarker.foofun.cn/dgui_quickstart_template.html
字符串、数字判断:
==(等于)、!= (不等于)<、
基本指令
if 指令
通常来说, 在指令或插值中没有被引号标注的内容都被视为变量的引用。
在模板中的字符串 只能 放在引号内
请注意,这里的数字 没有 放在引号内。 如果将("0")放在引号中, 那么FreeMarker就会将其误判为字符串了
示例:
1.<#if animals.python.price < animals.elephant.price>
Pythons are cheaper than elephants today.
</#if>
2.<#if animals.python.price < animals.elephant.price>
Pythons are cheaper than elephants today.
<#else>
Pythons are not cheaper than elephants today.
</#if>
3.<#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>
*特例:
如果变量本身就是布尔值(true/false),则可以直接让其作为 if 的 condition (判断条件,译者注):
<#if animals.python.protected>
Pythons are protected animals!
</#if>
list 指令
list 指令的一般格式为: <#list sequence as loopVariable>repeatThis</#list>。 repeatThis 部分将会在给定的 sequence 遍历时在每一项中重复, 从第一项开始,一个接着一个。在所有的重复中, loopVariable 将持有当前遍历项的值。 这个变量仅存在于 <#list ...> 和 </#list> 标签内。
list 指令将列表视为一个整体, 在 items 指令中的部分才会为每个水果重复。 如果我们有0个水果,那么在 list 中的所有东西都被略过了
示例:
<ul>
<#list misc.fruits as fruit>
<li>${fruit}
</#list>
</ul>
1.是如果我们有0个水果,它仍然会输出一个空的 <ul></ul>,而不是什么都没有。 要避免这样的情况,可以这么来使用
<#list misc.fruits>
<ul>
<#items as fruit>
<li>${fruit}
</#items>
</ul>
</#list>
2.使用一些分隔符来列出水果,比如逗号:
被 sep 覆盖的部分(我们也可以这么来写: ...<#sep>, </#sep></#list>) 只有当还有下一项时才会被执行。 因此最后一个水果后面不会有逗号。
<p>Fruits: <#list misc.fruits as fruit>${fruit} </#list>
3.如果我们有0个水果,会怎么样?只是打印 "Fruits:"(list 指令,也像 if 指令那样,可以有 else 部分,如果列表中有0个元素时就会被执行)
<p>Fruits: <#list misc.fruits as fruit>${fruit}<#sep>, <#else>None</#list>
4.所有的这些指令(list, items, sep, else)可以联合起来使用:
<#list misc.fruits>
<p>Fruits:
<ul>
<#items as fruit>