freemarker常用标签解释

标签一: if else

你可以使用if,elseif和else指令来条件判断是否越过模板的一个部分。这些condition-s必须计算成布尔值,否则错误将会中止模板处理。elseif-s和else-s必须出现在if的内部(也就是,在if的开始标签和技术标签之间)。if中可以包含任意数量的elseif-s(包括0个)而且结束时else是可选的。

<#if x == 1>
  x is 1
</#if>
<#if x == 1>
  x is 1
<#else>
  x is not 1
</#if>
<#if x == 1>
  x is 1
<#elseif x == 2>
  x is 2
<#elseif x == 3>
  x is 3
</#if>
<#if x == 1>
  x is 1
<#elseif x == 2>
  x is 2
<#elseif x == 3>
  x is 3
<#elseif x == 4>
  x is 4
<#else>
  x is not 1 nor 2 nor 3 nor 4
</#if>

当然你也可以嵌套使用

<#if x == 1>
  x is 1
  <#if y == 1>
  and y is 1 too
  <#else>
  but y is not
  </#if>
<#else>
  x is not 1
  <#if y < 0>
  and y is less than 0
  </#if>
</#if>

2 switch,case,default,break指令

<#switch being.size>
  <#case "small">
  This will be processed if it is small
  <#break>
  <#case "medium">   This will be processed if it is medium   <#break>
  <#case "large">   This will be processed if it is large   <#break>
  <#default>   This will be processed if it is neither </#switch>

3 list,break 指令

  

<#assign seq = ["winter", "spring", "summer", "autumn"]>
<#list seq as x>
  ${x_index + 1}. ${x}<#if x_has_next>,</#if>
</#list>

4 include指令

<#assign me = "Juila Smith">
<h1>Some test</h1>
<p>Yeah.
<hr>
<#include "/common/copyright.ftl">

5 compress指令

<#assign x = " moo \n\n ">
(<#compress>
1 2 3 4 5
${moo}
test only
I said, test only
</#compress>)

当你使用了对空白不敏感的格式(比如HTML或XML)时压缩指令对于移除多余的空白是很有用的。它捕捉在指令体(也就是在开始标签和结束标签中)中生成的内容,然后缩小所有不间断的空白序列到一个单独的空白字符。如果被替代的序列包含换行符或是一段空间,那么被插入的字符也会是一个换行符。开头和结尾的不间断的空白序列将会完全被移除。

上面输出结果为:

(1 2 3 4 5
moo
test only
I said, test only)

 

posted @ 2019-02-20 10:00  菩提树下的丁春秋  阅读(3727)  评论(0编辑  收藏  举报