In Velocity # set command,

Source: Internet
Author: User

In Velocity # set command,

The reference allows the template designer to generate dynamic content, and the Instruction allows the designer to be truly responsible for page presentation and content.

The command starts with #, followed by a keyword, such as if, else, and foreach. This keyword can be placed in the middle. This is a standard writing method, but it may not be easy to understand.

# Set

# The set command is used to assign values to a variable or attribute. The format is # set ($ foo = "bar") or # set ($ foo. bar = $ test ).

LHS must be a variable or attribute, and RHS must be one of the following seven types: Variable, attribute, String Literal Value, numeric literal value, method, ArrayList, and Map. For example:

# Set ($ test = $ test2 + 3)

# Set ($ test. a = "abc ")

# Set ($ test. B = 1)

# Set ($ test. c = $ test.)

# Set ($ test. d = $ test. calc ("1 + 1 "))

# Set ($ test. e = ["123", "abc", $ test. c]) use $ test. e. get (0) to get the value "123"

# Set ($ test. f = {"name": "car", "color": "red"}) $ test. f. get ("color") to get the value "red"

By default, if RHS is null, LHS will not be assigned, and LHS will keep the original value. Therefore, a reference cannot be removed once it is created. However, you can modify a configuration of Velocity to change this phenomenon. Pay special attention to # foreach when making judgments.

When # set is used, if the string literal value is placed in double quotation marks, it will be parsed. For example, for # set ($ test. a = "abc"), the value of $ test. a is abc. # Set ($ test. B = "$ test. a") is equivalent to # set ($ test. B = $ test. ). However, the result of # set ($ test. B = '$ test. A') is to assign the value of $ test. a to $ test. B.

You can use # literal ()... # end to enclose the VTL Code when you need to output a large number of character strings instead of their actual values.

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.