freemarker四種變量
1、簡單介紹說明
(1)數據模型中的變量:root中的變量
(2)模板中的變量:使用<#assign>定義的變量
(3)局部變量:在指令中的變量
(4)循環變量:在循環中的變量
2、使用說明
(1)數據模型中的變量:root中的變量
A Junit方法
@Test public void testRoot() { root.put("age", "23"); studentPrint("tag.ftl"); }
<html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>freemarker root中的變量</title> </head> <body> <#--freemarker數據模型中的變量--> ${age} </body> </html>
結果:
<html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>freemarker root中的變量</title> </head> <body> 23 </body> </html>
(2)模板中的變量:使用<#assign>定義的變量
<#--freemarker模板中的變量--> <#---此時模板中的變量的名稱和模型中的變量名稱一致,不覆蓋,而是隱藏--> <#assign age="56"/> ${age} <#--使用.globals能夠訪問模型中的變量--> ${.globals.age}
結果:
56 23
(3)局部變量:在指令中的變量
<#--freemarker模板中的變量--> <#---此時模板中的變量的名稱和模型中的變量名稱一致。不覆蓋,而是隱藏--> <#assign age="56"/> ${age} <#--使用.globals能夠訪問模型中的變量--> ${.globals.age} <#macro ageNum> <#local age="45"/> </#macro> <@ageNum/> ${age}
結果:
56 23 56
(4)循環變量:在循環中的變量
${age} <#list 1..10 as age> ${age} </#list> ${age}
結果:
56 1 2 3 4 5 6 7 8 9 10 56