freemarker学习笔记

内置的方法:
1.
 html: 特殊的 HTML 标记会被转义字符替代 (E.g. < with &lt;)

cap_first: The string with the first letter converted to upper case

lower_case: The lowercase version of the string

upper_case: The uppercase version of the string

trim: The string without leading and trailing white-spaces

size: The number of elements in the sequence

int: The integer part of a number (e.g. -1.9?int is -1)

${test?html}
${test?upper_case?html}  
 
test stores the string "Tom & Jerry"结果:
Tom &amp; Jerry
TOM &amp; JERRY  
 
 
${seasons?size}
${seasons[1]?cap_first} <#-- left side can by any expression -->
${"horse"?cap_first}  

seasons stores the sequence "winter", "spring", "summer", "autumn", the output will be:

4
Spring
Horse  
 
2.  
指定小数部分位数为 1: m1

指定小数位数最大为 3: M3

指定小数部分位数为 1, 但是小数位数最大可以为3: m1M3

For example, assume that x is 2.582 and y is 4:

  
            <#-- If the language is US English the output is: -->
#{x; M2}   <#-- 2.58 -->
#{y; M2}   <#-- 4    -->
#{x; m1}   <#-- 2.6 -->
#{y; m1}   <#-- 4.0 -->
#{x; m1M2} <#-- 2.58 -->
#{y; m1M2} <#-- 4.0  -->  
 
  
3.
可以指定macro中参数的默认值
you want to use "black" for color if you don't specify value for that parameter when you use the greet directive:
 <#macro greet person color="black">
  <font size="+2" color="${color}">Hello ${person}!</font>
</#macro>  
Now <@greet person="Fred"/> is OK
现在如果color属性没有指定将使用默认值 
  

4.
<#macro repeat count>
  <#local y = "test">
  <#list 1..count as x>
    ${y} ${count}/${x}: <#nested>
  </#list>
</#macro>
<@repeat count=3>${y?default("?")} ${x?default("?")} ${count?default("?")}<
/@repeat>  

will print this:
    test 3/1: ? ? ?
    test 3/2: ? ? ?
    test 3/3: ? ? ?  
 
注意<#list 1..count as x>这句的用法
  
5.
<#macro repeat count>
  <#list 1..count as x>
    <#nested x, x/2, x==count>
  </#list>
</#macro>
<@repeat count=4 ; c, halfc, last>
  ${c}. ${halfc}<#if last> Last!</#if>
<
/@repeat>  
 
The output will be:
 
  1. 0.5
  2. 1
  3. 1.5
  4. 2 Last!
  
相当于x, x/2, x==count 是 c, halfc, last的形参
这样也是可以的  
<@repeat count=4 ; c, halfc, last>
  ${c}. ${halfc}<#if last> Last!</#if>
<
/@repeat>
<@repeat count=4 ; c, halfc>
  ${c}. ${halfc}
<
/@repeat>
<@repeat count=4>
  Just repeat it...
<
/@repeat

 

6.Create and replace variables with assign:
  
 <#assign x = 1>  <#-- create variable x -->
${x}
<#assign x = x + 3> <#-- replace variable x -->
${x}  

Output: 
1
4  
 
  
7.name space
<#import "/lib/my_test.ftl" as my> <#-- hash called my will be the "gate" -->
<@my.copyright date="1999-2002"/>
${my.mail}

使用<#import "/lib/my_test.ftl" as my> 来代替<#include "/lib/my_test.ftl"> ,这样就可以利用命名空间了 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值