Beetl2.7 Chinese Document

Source: Internet
Author: User
Tags arithmetic map class naming convention switch case try catch
Beetl2.7 Chinese Document

Turn from: http://ibeetl.com/guide/

Beetl Author: Li Jiazhi [xiandafu@126.com] (mailto:xiandafu@126.com) 1. What is Beetl

Beetl current version is 2.7.23, relative to other Java template engine, with full-featured, intuitive syntax, high performance, and the preparation of templates easy to maintain and so on. Makes it a good experience to develop and maintain templates. is a new generation of template engines. Generally speaking, its features are as follows: full-featured: As the mainstream template engine, Beetl has a considerable number of features and other template engines do not have the functionality. Applies to a variety of scenarios, from large Web sites with high response requirements to a wide range of CMS management systems. Beetl itself also has a number of unique features to complete template authoring and maintenance, which other template engines do not have. Very simple: Similar to JavaScript grammar and custom, as long as half an hour can be half a half to learn the full use of guessing. Reject other template engines that impersonal grammar and custom. It also supports HTML tags, making it easier to develop CMS systems: Beetl far exceeds the mainstream Java Template engine performance (engine performance 5-6 times and freemaker,2 times the JSP). Reference appendix), and consumes a lower CPU. Easy to integrate: Beetl can be easily integrated with a variety of web frameworks, such as spring Mvc,jfinal,struts,nutz,jodd,servlet. Support templates are developed and tested individually, that is, in the MVC architecture, you can develop and test templates even without the M and C sections. Extensions and Personalization: Beetl supports custom methods, formatting functions, virtual properties, tags, and HTML tags. At the same time, Beetl also supports custom placeholders and control statement starter symbols that also enable users to create their own toolkit. Can be extended as a scripting engine, a rule engine that can customize the engine to achieve advanced functionality. about Performance

The

is Beetl6 as long as freemarker,2 times the JSP by comparing it to the mainstream template engine freemarker,vecloity and JSP. This is because on the macroscopic, through the optimized rendering engine, IO binary output, byte code attribute access enhancement, micro, save context contexts through one-dimensional array, static text merging processing, prevent Java from frequently creating and destroying arrays by reusing byte arrays, and using template caching, Methods such as runtime optimization. Details reference Appendix unique features

Beetl Some of the features that have been developed for the more than 10-year template engine are not available, these features are very useful for the development and maintenance of templates, such as custom placeholders and control statement starting symbols, which helps to reduce the inclination of template syntax for templates, such as in HTML templates, if defining control statement symbols is <!-- : And, then, most of the template files can be opened through the browser. Some users only adopt a single symbol @ (or a single symbol "~") as well as carriage return change as a control statement starting symbol, which can improve the development efficiency of the template can be independently tested. Without a real control layer and model layer, BEETL templates can be developed and tested separately to support looser MVC and strict MVC, and you can disallow the use of these syntaxes if you embed calculation expressions in the template language, complex conditional expressions, and function calls that interfere with business logic. Powerful secure output through a secure output symbol. , can be in the template variable, variable attribute reference, for loop, placeholder output, Try-catch medium each place provides the safe output, guarantees the rendering to be normal. Template variable: Run a part of the template output like JS to assign to a variable, later processing. The use of template variables to complete a very complex page layout (simple layout can use the include,layout tag function) Type speculation, can be used to speculate on the type of template variables, so as to optimize performance, but also through the annotation method to display the description template variable attributes (this is not necessary, But to help the IDE auto prompt feature pluggable design, error message hints, template engine caching mechanism, template resource management, locally invoked security Manager, strict MVC constraints, template engine itself has a default implementation, but can be customized to fit specific requirements enhanced syntax, such as For-elsefor, select-case, safe output symbols!, omitted ternary expressions, etc., these syntaxes are particularly suitable for template development of local rendering techniques, combining now JS Ajax technology. Ultra-high performance, with the fastest template interpretation engine, at the same time, there is a lower CPU consumption. 5-6 times the domestic use of freemaker. Suitable for various types of template applications, such as code generation tools, CMS systems, common Web sites, ultra-high access to the portal system, and rich client JS Framework integration of background management applicationshow to get started with little whiteNeed to read through the basic usage, most of them are explained grammar, and the syntax is very close to JS, so you can quickly preview, but Beetl is designed for templates, so like safe output, tags and HTML tags, global variables, temporary variables and shared variables, layout technology, And call Java code directly, and so on, you need to read it carefully. If you are engaged in web development, you also need to read the first section of "Web-provided global variables" in Web integration, and if you use AJAX technology on the web, you can read "Local rendering technology that consolidates Ajax." Contains spring,jfinal,jodd,struts such as demo can be used as a reference to learn Https://git.oschina.net/xiandafu any problem, can be asked in the ibeetl.com community. At present, the response rate is 100%, the question needs to specify their own expectations, error messages, attached code or picturesContact Author

Author: leisure. Da Fu (Li Jiazhi) etc (refer to the appendix to see the Code contributor)

QQ Technology Group: 219324263

Mail: xiandafu@126.com

Beetl Community: bbs.ibeetl.com

SOURCE page: https://github.com/javamonkey/beetl2.0

Online experience and code sharing http://ibeetl.com/beetlonline/ 2. Basic usage 2.1. Installation

If you are using MAVEN, use the following coordinates

<dependency>
        <groupId>com.ibeetl</groupId>
        <artifactid>beetl</artifactid >
        <version>2.7.23</version>
</dependency>

If not maven project, direct download Http://git.oschina.net/xiandafu/beetl2.0/attach_files 2.2. Starting from GroupTemplate

Stringtemplateresourceloader Resourceloader = new Stringtemplateresourceloader ();
Configuration cfg = configuration.defaultconfiguration ();
GroupTemplate GT = new GroupTemplate (Resourceloader, CFG);
Template t = gt.gettemplate ("Hello,${name}");
T.binding ("name", "Beetl");
String str = T.render ();
System.out.println (str);

Beetl Core is grouptemplate, create grouptemplate need two parameters, one is template resource loader, one is configuration class, template resource loader Beetl built-in 6 kinds, respectively Stringtemplateresourceloader: string template loader for loading string templates, as shown in this example fileresourceloader: The file template loader, which requires a root directory as a parameter construct, The string that is passed into the GetTemplate method is the relative path of the template file relative to the root directory classpathresourceloader: File template loader, template file located in Classpath Webappresourceloader: For WebApp integration, assume that the template root directory is the Webroot directory, reference Web integration Chapter Mapresourceloader: Can be stored dynamically in the template Compositeresourceloader mixed with multiple loading modes

Code 5th will pass the variable name into the template with the value "Beetl". Line 6th of the code is the render template, the output, template provides a variety of methods to obtain rendering output, the following Tempalte.render () returns the rendering results, as shown in this example Template.renderto (Writer) rendering results output to Writer Template.renderto (outputstream) render result output to outputstream about how to use the template resource loader, refer to the next section how to configure the template, refer to the next section if you don't want to write code to experience beetl directly, You can use http://ibeetl.com/beetlonline/ 2.3. Template Base Configuration

Beetl provides not only full-featured, but also a number of unique features, through a simple configuration file, you can define a number of features, by default, the configuration class will always load the default configuration file (located in/org/beetl/core/ Beetl-default.properties, as a novice, usually only need to focus on the configuration of the 3,4,5,6 row delimiter, as well as the configuration of the 12-line template character set , other configurations will be mentioned in later chapters, and for frameworks such as spring, Some of the configurations will be covered by the configuration of these frameworks, which need to be referenced in the following sections, with a fragment of the content as follows:

#默认配置 engine=org.beetl.core.engine.fastruntimeengine delimiter_placeholder_start=${DELIMITER_PLACEHOLDER_END=} delimiter_statement_start=<% delimiter_statement_end=%> direct_byte_output = FALSE HTML_TAG_SUPPORT = True HTML_ Tag_flag = # Html_tag_binding_attribute = var Native_call = TRUE Template_charset = UTF-8 Error_Handler = org.beetl.core.c Onsoleerrorhandler native_secuarty_manager= Org.beetl.core.DefaultNativeSecurityManager mvc_strict = FALSE #资源配置,
RESOURCE properties are limited to specific resourceloader resource_loader=org.beetl.core.resource.classpathresourceloader #classpath root paths resource.root=/#是否检测文件变化, development is appropriate with true, but the line is changed to False resource.autocheck= true #自定义脚本方法文件的Root目录和后缀 resource.functionroot  = Functions Resource.functionsuffix = html #自定义标签文件Root目录和后缀 resource.tagroot = Htmltag Resource.tagsuffix = tag ##### extension ############## # # Built-in method fn.date = org.beetl.ext.fn.DateFunction ... # #内置的功能包 fnp.strutil = Org.beetl.ext.fn.StringUti L ... # #内置的默认格式化函数 FTC.java.util.Date = Org.beetl.ext.format.
 DateFormat .... # tag Class tag.include= Org.beetl.ext.tag.IncludeTag

This configuration file provides an overall description of the functionality provided by Beetl

Line 2nd configures the engine implementation class, which can be done by default.

3rd, line 4 Specifies a placeholder, the default is ${}, or it can be specified as a different placeholder.

5th, line 6 Specifies the bounding symbol for the statement, by default <%%>, or by specifying other bounding symbols

Line 7th Specifies the IO output mode, which defaults to false, the usual character output, which can be set to true when considering high performance. Please refer to advanced usage in detail

8th, line 9 Specifies support HTML tags, and the symbol is #, the default configuration, template engine recognition < #tag ></#tag > Such HTML tags, and can call the corresponding tag function or template file. You can also specify other symbols, such as BG: then identify <BG:

Line 10th specifies that if the label attribute has Var, it is considered a label function that requires the binding variable to the template

Line 11th specifies that the local class is allowed to call directly

Line 12th Specifies that the template character set is UTF-8

Line 13th specifies the parsing class for the exception, which is Consoleerrorhandler, and he will print out the error message (System.out) in the background when an exception occurs in render.

Line 14th Specifies the security policy for the local class call

Line 15th is configured for strict MVC, which is typically set to false.

Line 18th Specifies the template resource loader that is used by default, and note that when BEETL is integrated with other MVC frameworks, the template loader is not necessarily based on this configuration, such as spring, whose resource_loader is based on the spring configuration

Line 20th to 22nd configures some properties of the template resource loader, such as setting the root path to/, that is, the top-level path of the classpath, and always detecting whether the template changes

Line 23rd configures the directory where the custom method resides and the file name suffix. Beetl supports both defining methods through Java classes and defining methods through template files

Line 26th configures the directory where the custom HTML tags reside and the file name suffix. Beetl supports both defining tags through Java classes and defining tags through template files

Line 31st Registers a date method whose implementation class is Org.beetl.ext.fn.DateFunction

Line 34th registers a method package Strutil, its implementation class Org.beetl.ext.fn.StringUtil, each public method of this class will be registered as a Beetl method

Line 37th registers a date format function

Line 40th registers an include tag function

The template developer can create a beetl.properties configuration file, at which point the profile will overwrite the default profile properties, for example, your delimiter considerations are <!--: and-->, then in beetl.properties Join a line and put this profile in the Classpath root directory. Configuration.defaultconfiguration () always loads the system defaults first, then loads the Beetl.properties configuration properties, and replaces the former configuration with a duplicate

  #自定义配置
  delimiter_statement_start=<!--:
  delimiter_statement_end=-->

2.4.0 new features: Beetl support through the template to complete the function, that is, template functions, or through the template to implement HTML tags (rather than write Java code), you can beetl.properties for this application set the different statement delimiters to the regular template to distinguish, as follows

  function_tag_limiter=<%; %>

Semicolon split, if the configuration file does not function_tag_limiter=, then template functions, HTML tags using the same Delimiter_statement_start,Delimiter_statement_ end 2.4. Template Resource Loader

The resource loader is the workshop class that obtains the resource instance based on string values, and the resource loader is also responsible for responding to calls from the template engine asking for changes to the template. For beginners, regardless of how the template resource loader is implemented, simply 2.4.1 the three types of template resource loaders provided by the system based on your own scenario . String Template Loader

During the creation of the GroupTemplate, if a stringtemplateresourceloader is passed in, it is allowed to get the template instance object by calling Gt.gettemplate (String template), as shown in 2.1 2.4.2. File Resource Template Loader

More often, template resources are managed in a file format and placed centrally in a single file directory (such as the WebApp template root may be web-inf/template), so you can use Fileresourceloader to load the template instance as follows:

String root = System.getproperty ("User.dir") +file.separator+ "template";
Fileresourceloader Resourceloader = new Fileresourceloader (Root, "utf-8");
Configuration cfg = configuration.defaultconfiguration ();
GroupTemplate GT = new GroupTemplate (Resourceloader, CFG);
Template t = gt.gettemplate ("/s01/hello.txt");
String str = T.render ();
System.out.println (str);

Line 1th Specifies the template root, that is, the template directory, line 2nd, located under Project Engineering, constructs a resource loader and specifies that the character set is UTF-8 (or unspecified, because the configuration file defaults to UTF-8); Line 5th loads the template 2.4.3 through the relative path/s01/hello.txt of the template . Classpath Resource Template Loader

There are also instances where the template resource is packaged into a jar file or put together with class, so you can use Classpathresourceloader to load the template instance, the following code:

Classpathresourceloader Resourceloader = new Classpathresourceloader ("org/beetl/sample/s01/");
Configuration cfg = configuration.defaultconfiguration ();
GroupTemplate GT = new GroupTemplate (Resourceloader, CFG);
Template t = gt.gettemplate ("/hello.txt");
String str = T.render ();
System.out.println (str);

The 1th line of code specifies the template root, which starts at the root when the template is searched, and if the new Classpathresourceloader ("template/") indicates that the template under template is searched. An empty constructor is used here to indicate that the search path is the root path and the character set default character set UTF-8.

Line 4th loads the template 2.4.4 through the relative path org/beetl/sample/s01/hello.txt of the template . WebApp Resource Template Loader

Webappresourceloader is the resource template loader for Web applications, and the default root path is the Webroot directory. It is also possible to set the root attribute to the template root path relative to the Webroot, from the security angle, and recommend it to the Web-inf directory

The following are the methods for initializing grouptemplate in jfinal integration

Configuration cfg = configuration.defaultconfiguration ();
Webappresourceloader Resourceloader = new Webappresourceloader ();
GroupTemplate = new GroupTemplate (Resourceloader, CFG);

Webappresourceloader assumes that Beetl.jar is located in the Web-inf/lib directory, so you can infer the Webroot path by webappresourceloader The path of the class to specify the template root path. This is generally true of all online environments, and if the development environment or other environments do not conform to this assumption, you need to call Resourceloader.setroot () to specify the template path 2.4.5. Customizing the resource template loader

Sometimes the template may come from a different directory of the file system, or a part of the template from a file system, the other part from the database, and some situation template may be a confusing template, you need to customize the resource load, inherit Resouceloader to implement template functionality, this part please refer to the Advanced section 2.5. Delimiters and placeholder symbols

Beetl template language similar to the JS language and custom, only need to put the beetl language into the bounding symbol, such as the default is <%%>, placeholders for static text embedded placeholders for output, the following is the correct example

<%
var a = 2;
var b = 3;
var result = A+b;
%>
Hello 2+3=${result}

never use placeholder symbols in delimiters, because placeholders are only embedded in static text, and the following example is an error example

<%
var a = "hi";
var c = ${a}+ "Beetl"; should be var c = a + "Beetl"
%>

Every time someone asks me why I can't run as an example, I always hate the inhuman grammar that velocity brings.

Delimiters and placeholders usually have other options, such as delimiter @ and carriage return (at which time, template configuration delimiter_statement_end= or delimiter_statement_end=null can) #: and carriage return line <!--: and- -> <!--# and-->. and?>

Placeholder--#{}-##

You can also agree with the team to choose the team's favorite delimiters and placeholder symbols.

The bounding symbol is an expression, and if the expression conflicts with a delimiter or placeholder, you can use the "\" symbol, such as

@for (user in users) {
e-mail is ${user.name}\@163.com
@}
${[1,2,3]}//Output a JSON list
${{key:1,value:2 \}  //Output a JSON map,} need to add \
2.6. Note

Beetl syntax is similar to JS syntax, so the annotation is the same as JS: Single note with//

Multi-line gaze with/**/

<%/
* Here is a definition variable */
var a = 3;//define a variable.

/* The following will be commented
%>

<% * *%>

Line 2nd is a multiline comment

Line 3rd is a single-line comment

Lines 5th through 8th take multiple lines of comments, so there is also a comment that the template will not handle 2.7. Temporary variable definition

The variable defined in the template becomes a temporary variable, which is similar to the variable defined by Var in JS, as shown in the following example

<%
var a = 3;
var b = 3,c = "abc", D=true,e=null;
var f = [1,2,3];
var g = {Key1:a,key2:c};
var i = a+b;
%>
2.8. Global variable definition

Global variables are variables passed in template.binding that can be accessed anywhere in the template, including the child templates. As in Java code

Template.binding ("List", Service.getuserlist ());

In the template
<% for
(user in list) {
%>
hello,${user.name};
<%}%>
2.9. Shared variables

A shared variable refers to a variable that can be referenced in all templates and can be passed through Grouptemplate.setsharedvars (Map sharedvars), which can be used anywhere in all templates

//.....
GroupTemplate GT = new GroupTemplate (Resourceloader, CFG);
map<string,object> shared = new hashmap<string,object> ();
Shared.put ("name", "Beetl");
Gt.setsharedvars (shared);
Template t = gt.gettemplate ("/org/beetl/sample/s0208/t1.txt");
String str = T.render ();
System.out.println (str);
t = gt.gettemplate ("/org/beetl/sample/s0208/t2.txt");
str = T.render ();
System.out.println (str);
T1.txt
Hi,${name}
//t2.txt
hello,${name}
2.10. Template Variables

A template variable is a special variable that can assign the output of any section of a template to that variable and allow it to be used elsewhere later, as in the following code

<%
var content = {
        var c = "1234";
        print (c);
%>
Template Other content:

<%};%>

Line 2nd defines a template variable content = {...}; This variable, like a temporary variable, can be used elsewhere, and the most common use is for complex layouts. Please refer to the Advanced Usage Layout 2.11. Reference Properties

Property references are an important part of the template, and Beetl supports the same way as JavaScript, as follows Beetl support through "." Number to access the properties of the object, if JavaScript is the same. If the user object has a getname () method, it can be accessed through ${xxx.name} in the template if the template variable is an array or list class, which can be accessed through [], such as ${userlist[0]} if the template variable is a map class, this can be done by [] To access, such as ${map["name"]}, and if the key value is a string type, you can also use ${map.name}. This is not recommended, because it will allow the template reader to mistakenly assume that it is a Pojo object Beetl also supports generic get methods. That is, if the object has a public object get (String key) method, you can use the "." or [] to access, such as ${activityrecord.name} or ${activityrecord["name"}, will call the Activityrecord get (String key) method. If the object has both specific attributes and generic get (this model is not designed in a way that is not worth encouraging), then the specific attribute has a higher priority. Beetl can also refer to properties by [], such as ${user["name"}, which is equivalent to ${user.name}. This is consistent with JavaScript. But it is recommended not to do so because it is easy for the person reading the template to mistake this as a map type Beetl can also define additional object properties. Instead of changing the Java object, this is called a virtual attribute, for example, for all collections, arrays have a common virtual property size. The Virtual property is ". ~" + Virtual property name

Template.binding ("List", Service.getuserlist ());
Template.binding ("Pagemap", Service.getpage ());

In the template
total ${list.~size}
<% for
(user in list) {
%>
hello,${user.name};
<%}%> the

current page ${pagemap[' page '}, all ${pagemap["Total"}
2.12. Attribute Assignment

Beetl2.7.0 begins to support object assignment, such as:

<%
var user = ....
User.name= "Joelli";
User.friends[0] = Getnewuser ();
user.map["name"] = "Joelli";
%>
2.13. Arithmetic Expression

Beetl supports arithmetic expressions and conditional expressions like JavaScript, such as +-*/% and (), and self-added + +, self-subtraction-

<%
var a = 1;
var b = "Hi";
var c = a++;
var d = a+100.232;
var e = (d+12) *a;
var f = 122228833330322.1112h
%>

Temporary variable type defined in Beetl the default corresponding Java type is int or double, and for templates, That's enough. If you need to define a long precision type (corresponding to Java bigdecimal), you need to add h at the end of the number to indicate that this is a long precision bigdecimal, and subsequent calculations and outputs and logical expressions will be considered in terms of the long precision type. 2.14. Logical Expression

Beetl support similar Javascript,java conditional expressions such as >,\<,==,!=,>=, \<= and!, and && | | , as well as three-dimensional expression, such as the following example

<%
var a = 1;
var b= "good";
var c = null;

if (a!=1&&b== "good" &&c==null) {
        ...
}
%>

Three-dimensional expressions can be simplified if they only consider the value of the true condition, and the following two rows are the same.

<%
 var  a = 1;
%>
${a==1? " OK ": '}
${a==1?" OK "}
2.15. Circular statements

Beetl supports rich loops such as for-in,for (EXP;EXP;EXP), while loops, and circular control statements break;continue; In addition, if you do not enter the For loop body, you can also execute the statement specified by Elsefor. 2.15.1. For-in

The for-in loop supports traversing the collection object, and for the list and array and iterator, the object is the collection object, and for map, the object is Map.entry, as follows two examples

<% for
(user in userlist) {
        print (userlp.index);
        Print (user.name);
}
%>

The third line of code USERLP is a beetl implicitly defined variable that can be used in a circular body. Its naming convention is the item name plus LP, which provides information about the current loop, such as Userlp.index 's current index, starting at 1 userlp.size The length of the collection Userlp.first Whether it is the first userlp.last whether the last Userlp.even index is an even userlp.odd index is odd

How to remember the suffix is LP, there are two tricks, English good is the abbreviation of the loop, pinyin is the wife's phonetic abbreviation, this allows programmers to write every time when they will think of the wife (whether there is no, haha)

The following are examples of map usage

<% for
(entry in map) {
        var key = Entry.key;
        var value = Entry.value;
        Print (value.name);
}
%>
2.15.2. for (EXP;EXP;EXP)

More common for rendering logic is the classic for loop statement, as shown in the following example

<%
var a = [1,2,3];
for (Var i=0;i<a.~size;i++) {
        print (a[i]);
}
%>
2.15.3. while

For the same while loop statement that is common to render logic, the following example

<%
var i = 0;
while (i<5) {
        print (i);
        i++;
}
%>
2.15.4. Elsefor

Unlike the usual programming languages, if you do not enter the loop body, there is no additional processing, the template rendering logic is more common if you do not enter the loop body, you need to do something, so for the for loop, there are elsefor to express if the loop body does not enter, then execute the elsefor statement

<%
var list = [];
For (item in list) {

}elsefor{
        print ("no record");
}
%>
2.16. Conditional Statement 2.16.1. If Else

Like JS, support if else, as the following example

<%
var a =true;
var b = 1;
if (a&&b==1) {

}else if (a) {

}else{

}
%>
2.16.2. Switch-case

Like JS, support Switch-case, the following example

<%
var b = 1;
Switch (b) {case
        0:
                print ("It ' s 0");
                break;
        Case 1:
                print ("It ' s 1");
                break;
        Default:
                print ("error");
%>

Switch variables can support any type, unlike JS, which is only cosmetic 2.16.3. Select-case

Select-case is an enhanced version of switch case. He allowed the case to have a logical expression, at the same time, do not need to break every case, the default encountered a conditional case execution after the exit.

<%
var b = 1;
Select (b) {case
        0, 1:
                print ("It ' s small int");
        Case 2, 3:
                print ("It's big int");
        Default:
                print ("error");
%>

There is no need for a variable after the Select, so the logical expression after the case will decide which one to execute. The format is

<%
Select {case
        boolexp,orboolexp2:
                dosomething ();
}
%>
<%
var b = 1;
select{case
        b<1,b>10:
                print ("It's Out of range");
                break;
        Case B==1:
                print ("It ' s 1");
                break;
        Default:
                print ("error");
%>
2.17. Try-catch

Typically, template rendering logic is rarely used for try-catch but given the complexity of the rendering logic and the control of the template, a try catch is provided, and the output is still guaranteed when the render fails

<%
try{
        callothersystemview ()
}catch (Error) {
        print ("Temporarily no Data");
}
%>

The error represents an exception, and you can get a possible error message by Error.message.

You can also omit the Catch section, so that an exception occurs, and no action is made . 2.18. Virtual Properties

A virtual property is also an object's property, is a virtual, non-model object's real property, and the advantage of this is that when the template requires additional properties for display but does not want to change the model, you can use this approach, such as Beetl built-in virtual properties. ~size for arrays and collection types.

${user.gender}
${user.~gendershowname}

~gendershowname is a virtual property whose internal implementation displays the gender according to the Boolean variable gender

How to complete the virtual properties refer to Advanced usage 2.19. function call

Beetl is built with a few utility functions that can be invoked anywhere in Beetl. The following example is a call to the date function, without arguments, to return the current date

<%
var date = date ();
var len = strutil.length ("CBD");
println ("len=" +len);
%>

Note the function name supports namespace, so the function called in line 3rd of the code is Strutil.length

The method of defining beetl is very easy, there are three ways to implement the call method of the function class, add to the configuration file, or display the Code registration Registerfunction (name,yourfunction) You can call Registerfunctionpackage (namespace,yourjavaobject) directly, at which point all public methods in Yourjavaobject will be registered as Beetl methods. The method name is Namespace+ "." + method names can be directly written to the template file and in HTML as a suffix, placed in the Root/functions directory, so that this template file is automatically registered as a function, its function name is the template file name.

For more information please refer to advanced usage

Beetl built-in functions Please refer to the Appendix for the following list of commonly used functions date Returns a java.util.Date type of variable, such as date () to return a current time (corresponding to the Java java.util.Date); ${date ("2011-1-1", "Yyyy-mm-dd")} returns the specified date print Prints one object print (User.Name); println prints an object and carriage return symbol, which uses the template itself instead of the local system. If you print only a newline character, call println () directly to NVL the function NVL, If the object is null, the second argument is returned, otherwise, returns the NVL (user,&quo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.