1. Distinction of Template Comments
| Syntax | Type | Features |
|---|---|---|
<!-- HTML comment --> | Native HTML comment | Output to final webpage source code, visible when viewing page source in browser |
## Single‑line comment | Velocity single‑line comment | Recognized only by the engine, not rendered to page, effective till end of line |
#* Multi‑line comment content *# | Velocity multi‑line comment | Supports line breaks, not rendered to page |
Development suggestion: Use
##/#* *#for business‑related code comments to avoid leaking comment content in page source.
2. Variable Naming Rules
Official basic naming rules
- The first character of variable must be an English letter (a‑z / A‑Z)
- Subsequent allowed characters: letters, digits
0‑9, underscore_, hyphen-
Hyphen - causes operator ambiguity
#set($name-d = "test")Code language: PHP (php)
It throws runtime error: Encountered "name" ... Was expecting one of:
Cause: Velocity interprets - as subtraction operator!
Engine parsing logic: $name - d, treated as variable $name minus variable d, leading to syntax conflict.
Two solutions:
- Recommended: use underscore naming (no ambiguity, common convention)
#set($name_d = "hello world")Code language: PHP (php)
- Use curly braces to define variable boundary (compatible with hyphen‑containing names)
#set(${name-d} = "hello world")
${name-d}Code language: PHP (php)
Valid / Invalid Examples Comparison
# ✅ Valid
#set($name_d = "underscore")
#set($name12d = "letter start plus number")
#set(${name-1_2d} = "hyphen requires curly braces")
# ❌ Direct hyphen without curly braces → parse error
#set($name-d = "Error!")
#set($name-1_2d = "Error!")Code language: PHP (php)
3. #set Syntax Specification
Standard writing (spaces around brackets are flexible, consistent style is suggested)
#set($variableName = "value")Code language: PHP (php)
String values must be wrapped in double quotes; keep spaces around equal sign for better readability.
Comments
Previous: Template reuse
Next: Iterate collections