Velocity (7) -- Advanced velocity usage and advanced velocity usage

Source: Internet
Author: User

Velocity (7) -- Advanced velocity usage and advanced velocity usage
1. truncate part of the field substring

1 original string: $! Ag. tagValue, which may be very long, must be truncated when the front-end page is displayed. 2 # set ($ str = $! Ag. tagValue) 3 4 # if ($ str. length ()> 30) 5 # set ($ str = $ str. substring (0, 30) 6 $ str... 7 # else8 $! Ag. tagValue9 # end

When using substring (begin, end), note that the end position is: end index-1.

2. split the data into arrays.
 1 #if($!{result.creType} && $result.creType) 2   #foreach ($element in $result.creType.split(",")) 3     #if($element == 1) 4        <script>document.getElementsByName("cretype")[0].checked="checked";</script> 5     #elseif($element == 2) 6        <script>document.getElementsByName("cretype")[1].checked="checked";</script> 7     #else 8        <script>document.getElementsByName("cretype")[2].checked="checked";</script>               9     #end10   #end11 #end 

3. When split into arrays, we need to look at several available methods of the array.

Arrays in velocity correspond to List objects in Java. For Java Native Array objects, only # foreach can be traversed. The $ arr [0] and $ arr. get (0) methods cannot be used.

4. Therefore, java never uses arrays, but list

-- In fact, list is very convenient, but list is much less efficient than array.

  • $ Myarray. isEmpty () list is empty
  • $ Myarray. size () Get the number of list elements
  • $ Myarray. get (2) get the elements of the specified list object
  • $ Myarray. add () add elements

5. $ velocityCount for counting

In foreach, velocityCount indicates the number of cycles. Starting from 1, it indicates the first loop.

6. null and null string "" Judgment

In velocity, non-null is considered true. Therefore, null can be used

1 # if ($! Variable name) 2 ...... 3 # else4 ...... 5 # end

Or you can judge null or empty strings.

1 # if ("$! VarName "! = "") 2 is not null or "" 3 # else4 ...... 5 # end

7. Basic Syntax Error # end do not forget

At the beginning, I often # if is finished and an error is reported. Later I finish writing if and then end, and finally write in the middle.

8. Macro# Macro is actually quite simple

I don't know how to touch it. Since I wrote a velocity page, I suddenly felt that macros should not be very useful. This is the case. At that time, the region-level three-level linkage had to be called in multiple places, extracted as a public module, and conflicts occurred after being imported using # parse, because a page was linked with the code of the region twice, the id is repeated. So I want to change the id to a parameter. When I need this public code, I need to transmit the id as a parameter. The result of render is that the html code id is my parameter. In this case, macro is a function.

If you want to use macros, write them first, parse them, and then call them.

A simple macro function:

1 #macro( d )2 <tr><td></td></tr>3 #end

Use it: If you directly # d on this page, if you write a template file separately, you need to first # parse (file path), and then # d, like js external reference.

Macro for passing parameters:

1 #macro( tablerows $color $somelist )2 #foreach( $something in $somelist )3 <tr><td bgcolor=$color>$something</td></tr>4 #end5 #end

A comprehensive use, paging.

9 Reference external files # include and # parse

I often started to use # include. Later I found that the page refresh did not change. I just needed to use # parse. Literally, one is taken directly, and the other is parsed. Therefore, if html code is uploaded, you can directly include it. If the velocity code is included, You need to compile # parse.

 

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.