freemarker常用标签解释

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 动手的程序员 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值