Beetl Precautions for use

Source: Internet
Author: User
Tags switch case try catch

1. If an expression conflicts with a delimiter or placeholder, you can use the "\" symbol

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

The temporary variable type defined in 2.Beetl corresponds to the default Java type int or double, which is commonly used for templates. That's enough. If you need to define a long precision type (corresponding to Java bigdecimal), you need to add H to the end of the number to indicate that this is a long-precision bigdecimal, and the subsequent calculations and outputs, as well as the logical expressions, will be considered by the long precision type.

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

3. Ternary expression If you only consider the value of the true condition, you can simplify the following two lines are the same effect.

var  A = 1 ; %>${a==1? " Ok ": '}${a==1?" OK "}

4. If you do not enter the For loop body, you can also execute the statement specified by Elsefor.

<%var list = [];  for inch list) {}elsefor{        print ("no Record");} %>

The 5.for-in loop supports iterating over the collection object, and for the list and array as well as iterator, the object is the collection object, and for the map, the object is Map.entry, the following two examples

<% for in userlist) {        print (userlp.index);        Print (user.name);} %>
<% for in map) {        var key = Entry.key;         var value = entry.value;        Print (value.name);} %>

6.userLP is a beetl implicitly defined variable that can be used in the loop body. Its naming specification is the item name followed by LP, which provides information about the current loop

Userlp.index: Current index, starting from 1 userlp.size: The length of the collection Userlp.first whether the first userlp.last is the last Userlp.even whether the index is even userlp.odd Whether the index is odd

7.Beetl can also locate additional object properties without changing the Java object, which is called a virtual property, such as, for all collections, arrays, there is a common virtual familiarity with size. Virtual property is ". ~" + Virtual 1 Property name

Template.binding ("list", Service.getuserlist ()), template.binding ("Pagemap", Service.getpage ()); // in the template total ${list.~size}<% for in list) {%>hello,${user.name}; <%}%> Current page ${pagemap[' page '}, ${pagemap["Total"}

8. Like JS, support Switch-case,switch variable can support any type, but not like JS can only be shaped

<%var b = 1; Switch (b) {        case 0:                print ("It ' s 0");                   Break ;          Case 1:                print ("It ' s 1");                  Break ;         default :                Print ("error");} %>

9.select-case is an enhanced version of the switch case. He allows a logical expression in a case, and does not require a break for each case, and exits when the conditional case executes by default.

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

A variable is not required after select, so that the logical expression after the case will decide which case to execute

<%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");} %>

10. Often the template rendering logic is seldom used in try-catch but given the complexity of the rendering logic and where the template is not controllable, a try catch is provided, and the output is guaranteed to be normal when rendering fails

<%try{        callothersystemview ()}catch(error) {        print ("No data Temporarily");} %>

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

11. Virtual properties are also properties of objects, but virtual, non-model object real properties, the advantage is that when the template needs additional for the display of properties but do 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

12. The safety output is a problem that any template engine must pay attention to, otherwise it will greatly bother the template developer. Beetl, if the template variable to be output is null, then Beetl will not do the output, which is different from the jsp,jsp output null, also different from Feemarker, if it is not used!, it will error.

There are two other cases in the template that cause the template output exception sometimes the template variable does not exist (such as in the child template) the template variable is null, but the output is a property of this variable, such as ${user.wife.name}
for the first two situations, you can add after the variable reference! To remind Beetl that this is a safe output variable. Like ${user.wife.name! }, even if user does not exist, or user is null, or user.wife is null, or user.wife.name is null beetl the output can be in ! after adding a constant (string, number type, etc.), or another variable, method, local call, as the default output, for example: ${user.wife.name! "single"}, if user is null, or user.wife is null, or user.wife.name is null, output "single" such as ${user.birthday! @System. Constants.defaultbir}, indicating if user is null, or user. Birthday is null, output System.constants.DefaultBir also has a situation that rarely occurs, but it is also possible that the output template variable occurs with any exception, such as an exception thrown inside the variable that needs to use the format ${! (variable)}, so that, in the event of any exception to the variable reference, no output, such as ${! (User.Name)},,beetl will call the User.getname () method, and if an exception occurs, Beetl will ignore the exception and continue rendering

13. Add it after the variable! Not only can it be applied to the placeholder output (but mostly to the placeholder output), or it can be used in an expression

<%<%var k = user.name! ' N/A ' +user.age! ; %>${k}%> If user is null, the K value will be N/a

14. In some templates, the entire template may require a safe output, or the part of the template needs to be safe, and the user does not have to use it for every expression! , you can use the Beetl security indicator to complete the security output such as:

<%DIRECTIVE Safe_output_open; %>${user.wife.name} template other content, can be safely output ... <%// turn off the safety output.  DIRECTIVE safe_output_close; %>

15. In the for-in loop, you can also add a safe output indicator to a set variable, so that if the set variable is NULL, you can also not enter the loop body

<%varnull;  for  in list! {}eslefor{        print ("no Data");} %>

16.2.21

Beetl Precautions for use

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.