这是最近几个月beetl 使用者完成的俩个电商网站截图:
最近在做中文翻译成英文以走出国门,没有翻译完,还有30多页了,虽然翻译的蹩脚,但看着已经有点像模像样了,等着慢慢优化,如果有自愿者能帮助,那就更好了
Beetl Guid
---Joel Li 2012-6-29
2.10. 直接调用 class方法和属性 .......................................................................... 11
3.2. 允许优化,超越其他模板引擎 ................................................................... 16
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.
${users[index]} |
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
File child = ….. Template template =group.getFileTemplate(child);
String output = template.getTextAsString();
} |
In this case, Beetl can parser the following template
<!--: var name= ”lijz ”; var greeting = “morning ”; --> <p>Hello ${name} ${greeting~}</p> |
Note: the default delimiter of statement are “<%” “%>” ,the default delimiter of placeholder are “${“ “ }”
Beetl have two kinds of variable.one is global variable which can be referred in any place( include child template) ,the global variable is readonly and set value by call template.set(String,Object) in Java code ,Another variable is temporary variable which only defined in Template such as
<%var name='lijz',loopCount=100+other ,girlName;%> |
(The keyword “var” is necessary , it’s differ from javascript) .
There is no scope for temporary variable( like java) ,the ,the following code will get error of “variable has defined “name” 。
<%var name= ’lijz ’,i=1; %> <%if(i>=1){%> <%var name='lucy';%> Hello,${name} <%}%> |
Beetl also support JSON variable like javascript. It’s intuitive but I recommend model is defined in business layer not view layer
<% var usersList=[{ “name ”: ”lijz ”, ”age ”:18},{ “name ”: ”lucy ”, ”age ”:16}];%> She’s ${userList[1][ “name ”]} |
As above template fragment ,there is two item in array “usersList” , each item include a Map ,so the value of key “name” is “lucy” for the second item .
Beetl always wrap number object (int,float,double,Bigdecimal) as BeeNumber to support large scale calculation, so don’t worry the high precision computation .I recomand DO not do calculation if this is a part of business logic since we must try to flow MVC principle.
NOTE
The variable name is followed Java(JavaScript) name convention, but can not define the variable name start with “__” since it’s for internal using.