Comments

1. Distinction of Template Comments

SyntaxTypeFeatures
<!-- HTML comment -->Native HTML commentOutput to final webpage source code, visible when viewing page source in browser
## Single‑line commentVelocity single‑line commentRecognized only by the engine, not rendered to page, effective till end of line
#* Multi‑line comment content *#Velocity multi‑line commentSupports 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

  1. The first character of variable must be an English letter (a‑z / A‑Z)
  2. 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:

  1. Recommended: use underscore naming (no ambiguity, common convention)
#set($name_d = "hello world")Code language: PHP (php)
  1. 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

Leave a Reply

Your email address will not be published. Required fields are marked *