SpringBoot整合Freemarker(二)
if分支
语法:
<#if condition>
...
<#elseif condition2>
...
<#elseif condition3>
...
<#else>
...
</#if>
例子:
<#if x = 1>
x is 1
</#if>
---------------------------------
<#if x = 1>
x is 1
<#else>
x is not 1
</#if>
switch分支
语法:
<#switch value>
<#case refValue1>
...
<#break>
<#case refValue2>
...
<#break>
<#case refValueN>
...
<#break>
<#default>
...
</#switch>
例子:
<#switch cloth.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>
list循环
<#list sequence as item>
...
<#if item = "spring"><#break></#if>
...
</#list>
例子:
<#assign seq = ["winter", "spring", "summer", "autumn"]> // assign 定义一个变量,这里定义了一个数组
<#list seq as x> // list循环遍历这个数组
${x_index + 1}. ${x}<#if x_has_next>,</#if>
</#list>
关键字:
item_index:是list当前值的下标,从0开始
item_has_next:判断list是否还有值
macro, nested, return
语法
<#macro name param1 param2 ... paramN>
...
<#nested loopvar1, loopvar2, ..., loopvarN> //有了这和标签:调用宏的时候,必须用双标签
...
<#return>
...
</#macro>
用例
<#macro test foo bar="Bar" baaz=-1>
Test text, and the params: ${foo}, ${bar}, ${baaz}
</#macro>
<@test foo="a" bar="b" baaz=5*5-2/>
<@test foo="a" bar="b"/>
<@test foo="a" baaz=5*5-2/>
<@test foo="a"/>
输出
Test text, and the params: a, b, 23
Test text, and the params: a, b, -1
Test text, and the params: a, Bar, 23
Test text, and the params: a, Bar, -1