发表时间:2012-08-13
最后修改:2012-08-13
这是最近几个月beetl
使用者完成的俩个电商网站截图:
最近在做中文翻译成英文以走出国门,没有翻译完,还有30多页了,虽然翻译的蹩脚,但看着已经有点像模像样了,等着慢慢优化,如果有自愿者能帮助,那就更好了
Beetl Guid
---Joel Li
2012-6-29
1.
What is Beetl
............................................................................................................
2
1.1.
What beetl can do for you
..........................................................................
3
2.
Basic
Usage.........................................................................................................
3
2.1.
Hello Beetl
..................................................................................................
3
2.2.
Delimiter
of Placeholder and Statment..........................................................
4
2.3.
Define variable.............................................................................................
5
2.4.
算术表达式
.................................................................................................
7
2.5.
逻辑表达式
.................................................................................................
7
2.6.
循环语句
.....................................................................................................
7
2.7.
条件语句
.....................................................................................................
9
2.8.
函数调用
...................................................................................................
10
2.9.
格式化
......................................................................................................
10
2.10.
直接调用
class方法和属性
..........................................................................
11
2.11.
错误处理
...................................................................................................
11
2.12.
安全输出
...................................................................................................
12
2.13.
注释
..........................................................................................................
12
2.14.
其他琐碎功能
............................................................................................
13
3.
高级用法
.................................................................................................................
15
3.1.
总是使用
GroupTemplate............................................................................
15
3.2.
允许优化,超越其他模板引擎
...................................................................
16
3.3.
允许
Byte直接输出
....................................................................................
17
3.4.
自定义函数
...............................................................................................
18
3.5.
格式化函数
...............................................................................................
19
3.6.
严格
MVC控制
..........................................................................................
20
3.7.
虚拟属性
...................................................................................................
21
3.8.
标签
..........................................................................................................
21
3.9.
宏
.............................................................................................................
22
3.10.
空格处理
...................................................................................................
23
3.11.
自定义错误处理
........................................................................................
24
4.
Spring MVC.............................................................................................................
24
4.1.
配置
ViewResolver......................................................................................
24
4.2.
模板中获取参数
........................................................................................
25
5.
在
Servlet中使用
Beetl.............................................................................................
26
5.1.
配置
Linstener............................................................................................
26
5.2.
编写
Servlet...............................................................................................
28
6.
附录:扩展包
..........................................................................................................
28
6.1.
函数
..........................................................................................................
29
6.2.
格式化函数
...............................................................................................
29
6.3.
标签
..........................................................................................................
29
6.4.
Freemarker功能对比
..................................................................................
30
6.5.
Freemarker性能比较
..................................................................................
33
6.5.1.
单线程:
............................................................................................
33
6.5.2.
并发
...................................................................................................
34
Beet
l
is abbreviation of Bee template language
,current version is 1.2M1
,total size 360K (include antlr runtime lib)
,
the features of beetl
as following:
1
very simple
:It is a JavaScript-like grammar with a few symbol
。Anyone who familiar with java or javascript can master very soon.
2
Full functionality
:
It
is fully functional template language, support lots of features which current
popular Template engine support ,such as FreeMarder
(ref 附录freemarker
功能对比
)
3
extremely high performance
:If enable compiling template to java class and enable direct byte outputting
,the speed of rendering template is fastest in popular template
engines
,the consume of system resource is lower than
other template engine
(ref 附录freemarker
功能对比
)
4
unique features
,self customized placeholder
,virtual
attribute for model
,,self customized function, formatter,
Tag and safe outputing etc
。these features are easy
understand and can solve many template rendering issue
5
Support both MVC and strict MVC
, you can
forbid
evaluate expression
and complicated logic expression if you think
these should be put in logic layer not view layer
,please
refer strictly enforces model-view separation
6 can be easily
integrated with other Web Framework, such as Spring, Servlet .etc.
in 1 minute
As
template language, you can make use of it in any place of
MVC
base on Java
,such as Web page
,Code generation. You also can learn Antlr if you are interesting in it
because Beetl is a template language based on
Antlr
package
org.bee.tl.samples;
import
org.bee.tl.core.GroupTemplate;
import
org.bee.tl.core.Template;
public
class
Helloworld
{
public
static
void
main(String[] args)
throws
Exception
{
String
input =
"hello,${name}!"
;
GroupTemplate
group =
new
GroupTemplate();/1
Template
template = group.getStringTemplate(input); /2
template.set(
"name"
,
"beetl"
);/3
String
output = template.getTextAsString();/4
System.
out
.println(output);
}
}
|
1
Create GroupTemplate with empty constructor
so it can make a String Template
。
In
most case
,template is
file, so you can create GroupTemplate with the file root of templates, for
example: GroupTemplate
group = new
GroupTemplate(“c:\templateroot”)
2 In
this case , the template is a string "hello,${name}!"
,so,
you can call method getStringTemplate to get Template instance.
“${
” “}
” are
placeholder in template
。The expression in placeholder is
evaluated and output to the StringWriter
If
template is a file, you must call method getFileTemplate
,pass
a relative path to root as the
parameter. For examples:
GroupTemplate
group = new GroupTemplate(“c:/templateroot”)
/* c:/templateroot/user/userList.html is template file */
Template template
=
group.getFileTemplate(“/user/userList.html”);.
3
template.set(
String key
,
Object value
) just define Template variable which can be
referenced in any place of
Template
,the
parameter String is variable name followed Java(Javascript) name convention
,the parameter Object is value of variable which can be any java Object
instance.
You can reference object
attribute by using “.” For example
${user.name},If
Objec is array or subclass of java.util.List or
,you
can refrence item using index,such as ${user.friends[0]},if Object is subclass
of Java.util.Map
,
you
can refrence the value by key,for example ${books[
‘thinking in java
’].author}
4 you can get the
output by call
template.getTextAsString()
or template.getText(Writer) or
template.getText(OutputStream os)
( NOTE
:only
OutputStream can be used if you
using advance features
direct byte
output
)。In
this example
,the output is hello,beetl!
By default
, “<%”
is the start delimiter of Statement
,%> is the end
delimiter of statement
<%
for(user in userList){ %>
hello,${user.name}
<%}%>
|
By default,
the Beetl expression between
"${"
and "}" will be evaluated and output.
Beetl can define
delimiter of placeholder and statement as your favorite. For example you can
define <!--:
--> as your delimiter
of statement in HTML template.
public static void main(String[] args) {
GroupTempate group = new GroupTemplate(root);
group.setStatementStart("<!--:");
group.setStatementEnd("-->");
group.setPlaceholderStart("${");
group.setPlaceholderStart("}");
//
or call
config method for simplify
//group.config("<!--","-->","${","}");
File child = …..
Template template =group.getFileTemplate(child);
String output = template.getTextAsString();
</
|