Supplementary notes on variable boundaries

Variable preset: #set ($valok = "hello foxdevelop")

Variable Boundary ${} vs . Access Distinction

CodeRendered OutputExplanation
$valokhello foxdevelopBasic variable output
$valok.comhello foxdevelop.comVelocity tries to call property/method cn on object valok; string has no cn method, raw variable plus suffix gets printed
${valok}.comhello foxdevelop.com{} locks variable boundary. After variable valok renders, literal .com is appended (recommended concatenation syntax)
$!valok.comhellofoxdevelop.com$! silent reference: behaves like $valok if variable exists; outputs empty string instead of raw $xxx when missing
${valok.com}blankAttempts 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

CodeRendered OutputExplanation
what$valokwhathello foxdevelopDirect 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 foxdevelopQuotes are plain text inside template, they do not trigger string parsing; variable renders normally
what'$valok'what’hello foxdevelopSame 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}whathello foxdevelopwhat✅ Curly braces lock boundary, fixes variable‑text merging issue

Summary

  1. Always use ${variableName} when variable merges with adjacent text
    Wrong: $valokwhat;Correct: ${valok}what
  2. Append literal suffix: use ${valok}.cn instead of ${valok.cn}
  3. $!variable silent reference: outputs blank if variable missing, raw $xxx will not appear
  4. Single / double quotes inside template HTML are plain charactersOnly 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

Leave a Reply

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