Variable preset: #set ($valok = "hello foxdevelop")
Variable Boundary ${} vs . Access Distinction
| Code | Rendered Output | Explanation |
|---|---|---|
$valok | hello foxdevelop | Basic variable output |
$valok.com | hello foxdevelop.com | Velocity tries to call property/method cn on object valok; string has no cn method, raw variable plus suffix gets printed |
${valok}.com | hello foxdevelop.com | {} locks variable boundary. After variable valok renders, literal .com is appended (recommended concatenation syntax) |
$!valok.com | hellofoxdevelop.com | $! silent reference: behaves like $valok if variable exists; outputs empty string instead of raw $xxx when missing |
${valok.com} | blank | Attempts to read property valok.com. String has no com property/method, variable resolves to nothing. Use $!{valok.com} for empty‑string output |
${valok}.cn ✅ Variable plus suffix concatenation${valok.cn} ❌ Access property cn under object valok
Single / Double Quotes & Variable Concatenation
| Code | Rendered Output | Explanation |
|---|---|---|
what$valok | whathello foxdevelop | Direct concatenation, Velocity auto‑detects variable boundary |
what$"valok" | what$”valok” | ❌ Invalid syntax. $ cannot be immediately followed by quote, variable cannot be parsed |
what"$valok" | what”hello foxdevelop“ | Quotes are plain text inside template, they do not trigger string parsing; variable renders normally |
what'$valok' | what’hello foxdevelop‘ | Same as above, single quotes are ordinary literal characters |
$valokwhat | $valokwhat | ❌ Variable unrecognizable! Velocity treats valokwhat as full variable name, prints raw text when not found |
${valok}what | hello foxdevelopwhat | ✅ Curly braces lock boundary, fixes variable‑text merging issue |
Summary
- Always use
${variableName}when variable merges with adjacent text
Wrong:$valokwhat;Correct:${valok}what - Append literal suffix: use
${valok}.cninstead of${valok.cn} $!variablesilent reference: outputs blank if variable missing, raw$xxxwill not appear- Single / double quotes inside template HTML are plain characters;Only inside #set assignments do single quotes (no variable parsing) and double quotes (parse variables) behave differently
#set($a = "$valok") // Double quotes: resolve variable, a=hello bamn
#set($a = '$valok') // Single quotes: skip variable resolution, a=$valokCode language: PHP (php)
Supplementary notes on variable boundaries
Previous: Iterate collections
Next: Extension methods