Velocity Syntax Guide
In programming, Velocity is a template language used to generate text output. It is commonly used in web applications to combine static templates with dynamic data and produce final documents like HTML, XML, and more.
Here are some common syntax examples for Velocity:
- Variable reference:
- $variable: referencing a variable.
- If the variable is null or empty, no exception will be thrown.
- Note:
- Comment: Single-line comment.
- # Comment content: Multiline comment.
- Control structures:
- #if(condition):if the condition is true, then execute the content within it.
- #if(condition): If the previous condition is not met and this condition is true, then execute the contents within.
- #else: If none of the previous conditions are met, then execute the contents within this block.
- #End: End the if statement block.
- Loop through the elements in the $list and assign the current element to $item.
- #End: End the foreach statement block.
- Set the value of a variable.
- #include(“template.vm”): It references another template file.
- Output content:
- $variable: outputs the value of the variable.
- $!variable: Outputs the value of the variable. If the variable is null or empty, it outputs an empty string.
- #if(condition): Check the condition and output true or false.
- Macro definition:
- #macro(macroName $param1 $param2): Define a macro that can accept parameters.
- #end: End the macro definition.
- #macroName($arg1 $arg2): invoking the macro and passing parameters.
- Arrays and lists:
- Retrieve the element at a specified index in the array using $array.get(index).
- Obtain the length of the list using $list.size().
- String manipulation:
- $string.length(): Obtain the length of the string.
- $string.substring(startIndex, endIndex): Extracts a substring from a string.
The above are some commonly used syntax of Velocity, which can be used to generate dynamic content and control the output results of templates based on conditions.