Velocity basic syntax

Source: Internet
Author: User

Concise velocity tutorial

Velocity is a Java-based template engine that allows anyone to reference objects defined by Java code simply using the template language. As a relatively complete template engine, velocity has powerful functions, but it also increases application complexity. I. Basic Syntax 1. "#" is the script statement used to identify velocity, including # Set, # If, # else, # End, # foreach, # End, # iinclude, # parse, # macro, etc.; for example: # If ($ info. IMGs) # else  # end2," $ "is used to identify an object (or be understood as a variable); for example: $ I, $ MSG, $ tagutil. options (...). 3. "{}" is used to clearly identify the velocity variable. For example, if $ someonename is displayed on the page, velocity uses someonename as the variable name, if our program wants to display the name character after the someone variable, the above label should be changed to $ {someone} Name. 4 ,"! "Is used to forcibly display non-existent variables as blank. For example, if the page contains $ MSG, the value of MSG is displayed if the MSG object has a value. If the MSG object does not exist, the $ MSG character is displayed on the page. This is not expected. In order to display a non-existent variable or an object whose value is null as a blank space, you only need to add a "!" before the variable name.. Example: $! MSG we provide five basic template script statements to meet the requirements of all application templates. These four template statements are simple and can be directly added by the interface designer. In many easyjweb application practices, we can see that only the following four simple template script statements can be summarized in all interface templates: 1, $! OBJ returns the object result directly. For example, the Java object MSG value is displayed in the HTML Tag. <P> $! MSG </P> display the value of the MSG object processed by the htmlutil object in the HTML tag <p >$! Htmlutil. dosomething ($! MSG) </P> 2. # If ($! OBJ) # else # End judgment statement. For example, in easyjweb open-source applications, we often see an example that prompts message MSG. # If ($ MSG) <SCRIPT> alert ('$! MSG '); </SCRIPT> # The End Script indicates that when the object MSG object exists, the content following <SCRIPT> is output. 3. # foreach ($ info in $ list) $ info. somelist # End cyclically reads the objects in the list of sets and processes them accordingly. For example, in easyjf open-source forum system, the homepage of the Forum (0.3) displays the HTML Interface Template script of popular topics: # foreach ($ info in $ hotlist1) <a href = "/bbsdoc. EJF? Easyjwebcommand = show & cid = $! Info. CID "target =" _ blank ">$! Info. Title </A> <br> # The End Script indicates that objects in the hotlist1 set are cyclically traversed and relevant content of objects is output. 4. # macro (macroname) # End Script Function (macro) calls. It is not recommended to use them in a large number in the Interface Template. For example, in the example of adding, deleting, modifying, and querying quickly generated by easyjweb tools, you can click the title bar of the List to display the results in ascending/descending order, this is a template content that we often see in easyjweb. Function (macro) definition, usually placed at the beginning # macro (orderpic $ type) # If ($ orderfield. equals ($ type)  # End # specific end calls include: <font color = "# ffffff"> title # orderpic ("title") </font> 5. File Inclusion # inclue ("template file name ") or # parse ("template file name") is mainly used to process pages with the same content, such as the top or tail content of each website. For more information, see easyjf open-source blog and easyjf open-source forum! For example: # parse ("/blog/top.html") or # include ("/blog/top.html") the difference between parse and include is that if the included file contains the velocity script tag, it will be further parsed, And the include will be displayed as is. When using # Set, do not declare the velocity script variable on the page view, that is, try to use as little as possible # Set. Sometimes we need to display the serial number in the page, and the program object does not contain this serial number attribute, you can define it yourself. As shown in a loop system, # Set ($ I = 0) # foreach ($ info in $ list) No: $ I # Set ($ I = $ I + 1) # endvelocity script syntax abstract 1. Declaration: # Set ($ Var = xxx) the left side can be the following variable reference string literal Property Reference Method reference number literal # Set ($ I = 1) arraylist # Set ($ arr = ["yt1 ", "T2"]) Arithmetic Operator 2. annotation: single line # XXX multiple lines # * xxx xxxx xxxxxxxxxx * # type referenced by references 3. Variable variables starts with "$, the first character must be a letter. Character followed by a VTL identifier. (.. Z or .. Z ). A variable can contain the following characters: Alphabetic (.. z, .. z) Numeric (0 .. 9) hyphen ("-") underscore ("_") 4. properties $ identifier. identifier $ user. name in name hashtable user. similar to: user. get ("name") 5. Methods Object User. getname () = $ user. getname () 6. Formal reference notation use {} to separate variable names from strings, for example, # Set ($ user = "CSY" }$ {user} Name returns csyname $ username $! Username $ and $! When the username cannot be found, $ username returns the string "$ username", while $! Username returns an empty string "" 7. Double quotation marks and quotation marks # Set ($ Var = "HELO ") test "$ Var" returns testhello test' $ var 'and returns test' $ var'. You can set stringliterals. interpolate = false change the default Processing Method 8. conditional statement # If ($ Foo) <strong> velocity! </Strong> # End # If ($ Foo) # elseif () # else # End is executed when $ foo is null or a Boolean object's false value. 9. logical operators: ==&||! 10. Loop statement # foreach ($ VaR in $ arrays) // The set contains the following three vectors, A hashtable or an array # End # foreach ($ product in $ allproducts) <li> $ product </LI> # End # foreach ($ key in $ allproducts. keyset () <li> key: $ key-> value: $ allproducts. get ($ key) </LI> # End # foreach ($ customer in $ customerlist) <tr> <TD> $ velocitycount </TD> <TD> $ customer. name </TD> </tr> # define the end11 and velocitycount variables in the configuration file # default name of the loop counter # variable reference. directive. foreach. counter. name = velocitycount # default starting value of the loop # counter variable reference. directive. foreach. counter. initial. value = 112. Include File # include ("one.gif", "two.txt", "three.htm") 13. parse import script # parse ("me. VM ") 14. # stop the execution and return 15. Define the macro velocimacros, which is equivalent to the function supporting the inclusion function # macro (d) <tr> <TD> </tr> # End call # D () 16. macros with parameters # macro (tablerows $ color $ somelist) # foreach ($ something in $ somelist) <tr> <TD bgcolor = $ color> $ something </TD> </tr> # End # end17, range operator # foreach ($ Foo in [1 .. 5])

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.