freemarker中文手册文档

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

FreeMarker
FreeMarker Java
FreeMarker HTML Web MVC FreeMarker Java FreeMarker
FreeMarker Web Web
FreeMarker HTTP Servlet FreeMarker Web
FreeMarker Model2 Struts JSP
FreeMarker
1
HTML XML RTF Java
Servlet
Email Web Web
2
include if/elseif/else
HTML
3
FreeMarker Java Java
JavaBean XML SQL
4 Web
Web HTML
Model2 Web JSP
JSP
MVC
5
UNICODE
US
6 XML
<#recurse> <#visit> 2.3 XML
XML
FreeMarker (1)
1
1 + =
FreeMarker
—— HTML Web
Web
HTML FreeMarker
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome ${user}!</h1>
<p>Our latest product:
<a href="${latestProduct.url}">${}</a>!
</body>
</html>
HTML ${…} FreeMarker FreeMarker Template user latestProduct.url data model
(root)
|
+- user = "Big Joe"
|
+- latestProduct
|
+- url = "products/greenmouse.html"
|
+- name = "green mouse"
latestProduct user url name url name latestProduct
FreeMarker <html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Welcome Big Joe!</h1>
<p>Our latest product:
<a href="products/greenmouse.html">green mouse</a>!
</body>
</html>
2
(root)
|
+- animals
| |
| +- mouse
| | |
| | +- size = "small" | | |
| | +- price = 50
| |
| +- elephant
| | |
| | +- size = "large" | | |
| | +- price = 5000
| |
| +- python
| |
| +- size = "medium" | |
| +- price = 4999
|
+- test = "It is a test"
|
+- whatnot
|
+- because = "don't know"
hashes
scalars
scalars
scalars root “. animals.mouse.price
sequences hashes
(root)
|
+- animals
| |
| +- (1st)
| | |
| | +- name = "mouse"
| | |
| | +- size = "small"
| | |
| | +- price = 50
| |
| +- (2nd)
| | |
| | +- name = "elephant" | | |
| | +- size = "large"
| | |
| | +- price = 5000
| |
| +- (3rd)
| |
| +- name = "python"
| |
| +- size = "medium"
| |
| +- price = 4999
|
+- whatnot
|
+- fruits
|
+- (1st) = "orange"
|
+- (2nd) = "banana"
scalars animals[0].name
3
FreeMarker
${…} interpolations FreeMarker
FTL FreeMarker HTML HTML # @
<#-- --> <!-- -->
if
<#if animals.python.price < animals.elephant.price> Pythons are cheaper than elephants today.
<#else>
Pythons are not cheaper than elephants today.
</#if>
list
<p>We have these animals:
<table border=1>
<tr><th>Name<th>Price
<#list animals as being>
<tr><td>${}<td>${being.price} Euros
</#list>
</table>
<p>We have these animals:
<table border=1>
<tr><th>Name<th>Price
<tr><td>mouse<td>50 Euros
<tr><td>elephant<td>5000 Euros
<tr><td>python<td>4999 Euros
</table>
include
<html>
<head>
<title>Test page</title>
</head>
<body>
<h1>Test page</h1>
<p>Blah blah...
<#include "/copyright_footer.html"> </body>
</html>
<p>We have these animals:
<table border=1>
<tr><th>Name<th>Price
<#list animals as being>
<tr>
<td>
<#if being.size = "large"><b></#if>
${}
<#if being.size = "large"></b></#if>
<td>${being.price} Euros
</#list>
</table>
FreeMarker (3)
3
1
FTL FreeMarker
Interpolation ${ } #{ }
FTL FreeMarker HTML #
<#-- -->
<html>[BR]
<head>[BR]
<title>Welcome!</title>[BR]
</head>[BR]
<body>[BR]
<#-- Greet the user with his/her name -->[BR]
<h1>Welcome ${user}!</h1>[BR]
<p>We have these animals:[BR]
<ul>[BR]
<#list animals as being>[BR]
<li>${} for ${being.price} Euros[BR]
</#list>[BR]
</ul>[BR]
</body>[BR]
</html>
[BR]
FTL list FTL List ${name} ${NAME}
Interpolation
FTL FTL
<#if <#include 'foo'>='bar'>...</if>
FTL Interpolation
<h1>Welcome ${user <#-- The name of user -->}!</h1>[BR]
<p>We have these animals:[BR]
<ul>[BR]
<#list <#-- some comment... --> animals as <#-- again... --> being>[BR] ...
2
FreeMarker FTL
FTL HTML
<#directivename parameters>
</#directivename>
<#directivename parameters/>
@ # <@mydirective>...</@mydirective>
FTL <ul>
<#list animals as being>
<li>${} for ${being.price} Euros
<#if use = "Big Joe">
(except for you)
</#list>
</#if><#-- WRONG! -->
</ul>
FreeMarker
FreeMarker FTL <#list[BR]
animals as[BR]
being[BR]
>[BR]
${} for ${being.price} Euros[BR]
</#list >
< </
3
${"It's \"quoted\" and
this is a backslash: \\"}
${'It\'s "quoted" and
this is a backslash: \\'}
It's "quoted" and
this is a backslash: \
It's "quoted" and
this is a backslash: \
\" (u0022)
\' (u0027)
\\ (u005C)
\n (u000A)
\r Return (u000D)
\t Tab (u0009)
\b Backspace (u0008)
\f Form feed (u000C)
\l<
\g>
\a&
\{{
\x Code4 16 Unicode
raw \ { r ${r"${foo}"}
${r"C:\foo\bar"}
${foo}
C:\foo\bar
“.
“1E3
0 “.5
8 +8 08 8.00
true false
<#list ["winter", "spring", "summer", "autumn"] as x>
${x}
</#list>
winter
spring
summer
autumn
[2 + 2, [1, 2, 3, 4], "whatnot"]
2..5 [2, 3, 4, 5]
5..2
hash
/
{"name":"green mouse", "price":150}
${variable} $ @ #
(root)
|
+- book
| |
| +- title = "Breeding green mouses"
| |
| +- author
| |
| +- name = "Julia Smith"
| |
| +- info = "Biologist, 1923-1985, Canada"
|
+- test = "title"
book["author"].name
book.author.["name"]
book["author"]["name"]
[startIndex..endIndex] startIndex endIndex
FreeMarker .variablename
Interpolation
${..} #{..} ${"Hello ${user}!"}
${"${user}${user}${user}${user}"}
+
${"Hello " + user + "!"}
${user + user + user + user}
${..}
<#if ${isBig}>Wow!</#if>
<#if "${isBig}">Wow!</#if>
<#if isBig>Wow!</#if>
user “Big Joe ${user[0]}${user[4]}
${user[1..4]}
BJ
ig J
+ <#list ["Joe", "Fred"] + ["Julia", "Kate"] as user> - ${user}
</#list>
- Joe
- Fred
- Julia
- Kate
+ key <#assign ages = {"Joe":23, "Fred":25} + {"Joe":30, "Julia":18}>
- Joe is ${ages.Joe}
- Fred is ${ages.Fred}
- Julia is ${ages.Julia}
- Joe is 30
- Fred is 25
- Julia is 18
${x * x - 100}
${x / 2}
${12 % 10}
x 5
-75
2.5
2
${3 * "5"} <#-- WRONG! -->
+ ${3 + "5"}
35
int
${(x/2)?int}
${1.1?int}
${1.999?int}
${-1.1?int}
${-1.999?int}
x 5
2
1
1
-1
-1
= == !=
= != <#if 1 = "1">
Freemarker "x" "x " "X"
< <= > >=
Freemarker > FTL > >= <#if (x > y)>
lt lte gt gte < <= > >=
&& and || or ! not
<#if x < 12 && color = "green">
We have less than 12 things, and they are green.
</#if>
<#if !hot> <#-- here hot must be a boolean -->
It's not hot.
</#if>
“? “.
html HTML
cap_first
lower_case
upper_case
trim
size
int -1.9?int -1 test "Tom & Jerry"
${test?html}
${test?upper_case?html}
Tom &amp; Jerry
TOM &amp; JERRY
[subvarName] [subStringRange]. (methodParams) +expr -expr !
?
* / %
+ -
< > <= >= lt lte gt gte
== = !=
and &&
or ||
..
4 Interpolation
Interpolation
Interpolation ${expr}
Interpolation #{expr} #{expr; format}
Interpolation
Interpolation
#setting string Interpolation <#setting number_format="currency"/>
<#assign answer=42/>
${answer}
${answer?string} <#-- the same as ${answer} -->
${answer?string.number}
${answer?string.currency}
${answer?string.percent}
$42.00
$42.00
42
$42.00
4,200%
#setting string Interpolation ${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}
${lastUpdated?string("EEE, MMM d, ''yy")}
${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}
2003-04-08 21:24:44 Pacific Daylight Time
Tue, Apr 8, '03
Tuesday, April 08, 2003, 09:24:44 PM (PDT)
#setting
string Interpolation
<#assign foo=true/>
${foo?string("yes", "no")}
yes
Interpolation #{expr; format} format mX X
MX X
<#-- If the language is US English the output is: -->
<#assign x=2.582/>
<#assign y=4/>
#{x; M2} <#-- 2.58 -->
#{y; M2} <#-- 4 -->
#{x; m1} <#-- 2.6 -->
#{y; m1} <#-- 4.0 -->
#{x; m1M2} <#-- 2.58 -->
#{y; m1M2} <#-- 4.0 -->
FreeMarker (4)
4
1
macro
<#macro greet>
<font size="+2">Hello Joe!</font>
</#macro>
@ FTL #
<@greet></@greet>
<@greet/>
macro
<#macro greet person>
<font size="+2">Hello ${person}!</font>
</#macro>
<@greet person="Fred"/> and <@greet person="Batman"/>
<font size="+2">Hello Fred!</font>
and <font size="+2">Hello Batman!</font>
FTL
<@greet person=Fred/>
Fred person
<#macro greet person color>
<font size="+2" color="${color}">Hello ${person}!</font>
</#macro>
<@greet person="Fred" color="black"/>
<@greet color="black" person="Fred"/>
macro <@greet person="Fred" color="black" background="green"/>
<@greet person="Fred"/>
<#macro greet person color="black">
<font size="+2" color="${color}">Hello ${person}!</font>
</#macro>
<@greet person="Fred"/>
<#nested>
<#macro border>
<table border=4 cellspacing=0 cellpadding=4><tr><td>
<#nested>
</tr></td></table>
</#macro>
<@border>The bordered text</@border>
<table border=4 cellspacing=0 cellpadding=4><tr><td> The bordered text
</tr></td></table>
<#nested>
<#macro do_thrice>
<#nested>
<#nested>
<#nested>
</#macro>
<@do_thrice>
Anything.
</@do_thrice>
Anything.
Anything.
Anything.
FTL <@border>
<ul>
<@do_thrice>
<li><@greet person="Joe"/>
</@do_thrice>
</ul>
</@border>
<table border=4 cellspacing=0 cellpadding=4><tr><td> <ul>
<li><font size="+2">Hello Joe!</font>
<li><font size="+2">Hello Joe!</font>
<li><font size="+2">Hello Joe!</font>
</ul>
</tr></td></table>
<#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>
test 3/1: ? ? ?
test 3/2: ? ? ?
test 3/3: ? ? ?
nested <@…>
<#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>
1. 0.5
2. 1
3. 1.5
4. 2 Last!
2
plain include assign
local
list
plain plain
<#assign x = "plain">
1. ${x} <#-- we see the plain var. here -->
<@test/>
6. ${x} <#-- the value of plain var. was not changed -->
<#list ["loop"] as x>
7. ${x} <#-- now the loop var. hides the plain var. -->
<#assign x = "plain2"> <#-- replace the plain var, hiding does not mater
here -->
8. ${x} <#-- it still hides the plain var. -->
</#list>
9. ${x} <#-- the new value of plain var. -->
<#macro test>
2. ${x} <#-- we still see the plain var. here -->
<#local x = "local">
3. ${x} <#-- now the local var. hides it -->
<#list ["loop"] as x>
4. ${x} <#-- now the loop var. hides the local var. -->
</#list>
5. ${x} <#-- now we see the local var. again -->
</#macro>
1. plain
2. plain
3. local
4. loop
5. local
6. plain
7. loop
8. loop
9. plain2
<#list ["loop 1"] as x>
${x}
<#list ["loop 2"] as x>
${x}
<#list ["loop 3"] as x>
${x}
</#list>
${x}
</#list>
${x}
</#list>
loop 1
loop 2
loop 3
loop 2
loop 1
global user Big Joe
<#assign user = "Joe Hider">
${user} <#-- prints: Joe Hider -->
${er} <#-- prints: Big Joe -->
3
lib/my_test.ftl
<#macro copyright date>
<p>Copyright (C) ${date} Julia Smith. All rights reserved.
<br>Email: ${mail}</p>
</#macro>
<#assign mail = "jsmith@">
import Freemarker import
<#import "/lib/my_test.ftl" as my>
<#assign mail="fred@">
<@my.copyright date="1999-2002"/>
${my.mail}
${mail}
<p>Copyright (C) 1999-2002 Julia Smith. All rights reserved.
<br>Email: jsmith@</p>
jsmith@
fred@
assign <#import "/lib/my_test.ftl" as my>
${my.mail}
<#assign mail="jsmith@" in my>
${my.mail}
jsmith@
jsmith@
<#macro copyright date>
<p>Copyright (C) ${date} ${user}. All rights reserved.</p>
</#macro>
<#assign mail = "${user}@">
user Fred
<#import "/lib/my_test.ftl" as my>
<@my.copyright date="1999-2002"/>
${my.mail}
<p>Copyright (C) 1999-2002 Fred. All rights reserved.</p> Fred@。

相关文档
最新文档