天天看點

freemarker四種變量

freemarker四種變量

1、簡單介紹說明

(1)資料模型中的變量:root中的變量

(2)模闆中的變量:使用<#assign>定義的變量

(3)局部變量:在指令中的變量

(4)循環變量:在循環中的變量

2、使用說明

      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>
      
<#--freemarker模闆中的變量-->
     <#---此時模闆中的變量的名稱和模型中的變量名稱一緻,不覆寫,而是隐藏-->
	 <#assign age="56"/>
	 ${age}
	 
	 <#--使用.globals能夠訪問模型中的變量-->
	 ${.globals.age}      
56
	 
	 23      
<#--freemarker模闆中的變量-->
     <#---此時模闆中的變量的名稱和模型中的變量名稱一緻。不覆寫,而是隐藏-->
	 <#assign age="56"/>
	 ${age}
	 
	 <#--使用.globals能夠訪問模型中的變量-->
	 ${.globals.age}
	 
	 <#macro ageNum>
	      <#local age="45"/>
	 </#macro>
	 
	 <@ageNum/>
	 ${age}      
56
	 
	 23
	 
	 
	 56      
${age}
	 <#list 1..10 as age>
	     ${age}
	 </#list>
	 ${age}      
56
	     1
	     2
	     3
	     4
	     5
	     6
	     7
	     8
	     9
	     10
	 56