Review FreeMarker
//1. Create a configuration instance
Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
cfg.setDirectoryForTemplateLoading(new File("templates")); //templates is the parallel of src
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
//2. Get the template
Template t1 = cfg.getTemplate("a.ftl");
//3.Create a data-model
Map<String, Object> rt = new HashMap<String, Object>();
rt.put("user","Jetma");
rt.put("random", Math.random()*100);
List<String> list = new ArrayList<String>();
list.add("sss");
list.add("s02");
rt.put("lst",list);
rt.put("date1", new Date());
rt.put("html1", "<h1>H1</h1>");
// 4
Writer out = new OutputStreamWriter(System.out);
t1.process(rt,out);
out.flush();
out.close();
-------------------------------
a.ftl:
${user}, welcome!<#-- comments -->
<#if user!="xx">you are a new one!</#if>
<#if random gte 60>${random} >= 60
<#else>${random} lt 60<#-- gt gte lt lte -->
</#if>
<#list lst as i1 >${i1},</#list>
//for include file
<#include "includedxx.txt" />
<@m1 /><@m1 /><#-- invode ml -->
<@m2 a="par1" b="par2" c="par3"/>
<@border>ne
<@m2 a="nested1" b="nested2" c="r3"/>
</@border>
<#import "b.ftl" as bb/>
<@bb.copyright date="2016-03-01"/>
${bb.mail}
<#assign mail="my@google.com"/>
${mail}
<#assign mail="bb@bb.com" in bb/>
${bb.mail}
${date1?string("yyyy-MM-dd HH:mm:ss")}
<#assign html2="<b>Bold</b>"/>
${html2?html}
${html1?html}
${html1?cap_first}
${html1?lower_case}
${html1?upper_case}
-------------------
${sss!}<#-- if sss not define, default: "" -->
${sss! "abc"}<#-- if sss not define, default: "abc"-->
<#if user??><h1>${user}</h1></#if> <#-- Returns true if the user is not indicated, and false otherwise-->
<#if sss??><h1>sss ok</h1><#else> sss is not ok. </#if>
-----------------------------------------------------
includedxx.txt:
included xx
<#macro m1><#-- define macro command -->
<b>macro m1 testing 1</b>
</#macro>
<#macro m2 a b c><#-- define macro command -->
<b>param:${a}--${b}--${c}</b>
</#macro>
nested command
<#macro border>
<table border=1><tr><td>
<#nested>
</td></tr></table>
</#macro>
-----------------------------------------------------
b.ftl:
<#macro copyright date>
<p>Copyright (C) ${date} xxx.</p>
</#macro>
<#assign mail="xx@yahoo.com">
-----------------------------------------------------
Ref:
http://freemarker.incubator.apache.org/docs/pgui_quickstart_createconfiguration.html
http://freemarker.incubator.apache.org/docs/pgui_quickstart_createdatamodel.html
1. string and number
<#if str == "success">
xxx
</#if>
<#if str !== "error">
xxx
</#if>
2. List
<#if (list?size>0) >
</#if>
3. string
<#if infos.name?length lt 7 >${infos.name}
<#else>
${infos.name[0..6]}...
</#if>
4. if...else...
4. 1
<#if condition>
...
<#elseif condition2>
...
<#elseif condition3>
...
<#else>
...
</#if>
4. 2...
<#elseif condition2>
...
<#elseif condition3>
...
<#else>
...
</#if>
<#if x = 1>
x is 1
</#if>
4. 3 elseifx is 1
</#if>
<#if x = 1>
x is 1
<#elseif x = 2>
x is 2
<#elseif x = 3>
x is 3
</#if>
4.4x 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>
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>
5 switch...case...default...
<#switch value>
<#case refValue1>
...
<#break>
<#case refValue2>
...
<#break>
<#case refValueN>
...
<#break>
<#default>
...
</#switch>
5. 1<#case refValue1>
...
<#break>
<#case refValue2>
...
<#break>
<#case refValueN>
...
<#break>
<#default>
...
</#switch>
<#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>
5. 2This 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>
<#switch x>
<#case x = 1>
1
<#case x = 2>
2
<#default>
d
</#switch>
<#case x = 1>
1
<#case x = 2>
2
<#default>
d
</#switch>
6.
<#list sequence as item>
...
</#list>
item_index,item_has_next,...
</#list>
<#assign seq = ["winter", "spring", "summer", "autumn"]>
<#list seq as x>
${x_index + 1}. ${x}<#if x_has_next>,</#if>
</#list>
break
<#list seq as x>
${x_index + 1}. ${x}<#if x_has_next>,</#if>
</#list>
评论
发表评论