Escape characters and #if conditionals

Backslash Escape + #if Conditional Syntax

1. Backslash \ Escape Rules

Pre‑defined variable:#set($dog = "woof woof")

VTL CodeRendered OutputExplanation
$dogwoof woofNormal variable resolution
\$dog$dogSingle backslash: escapes the following $, disables variable parsing and prints $ literally
\\$dog\woof woofFirst \ escapes second \ → outputs \;
remaining $dog resolves normally
\\\$dog\$dog\\ outputs \, \$ outputs $
\\\\$dog\woof woof\\ outputs \, \\ outputs \;
$dog resolves variable
  1. Velocity variable names must start with a letter$10.0 will not be treated as variable and outputs as‑is;so writing Microsoft $10.0 USD in text requires no extra escaping;
  2. Consecutive backslashes cancel out in pairs;only the \$ combination can escape dollar‑sign;
  3. To display $variableName literally on page, standard syntax:\$dog.

2. #if Branch Conditional Syntax

#set($ages = 22)
#if ($ages <13)
    <h1>You are a child</h1>
#elseif ($ages <18)
    <h1>You are a teenager</h1>
#elseif ($ages <35)
    <h1>You are a young adult</h1>
#elseif ($ages <60)
    <h1>You are middle‑aged</h1>
#else
    <h1>You are elderly</h1>
#endCode language: PHP (php)
Execution Result

$ages=22, satisfies 22 < 35 → output:<h1>You are a young adult</h1>

Notes
  1. You must close block with #end, missing it will trigger parsing error;
  2. Keyword:#elseifdo NOT write as #else if, that is invalid syntax);
  3. Supported operators:< > <= >= == != && || and more;
  4. Matching rule: evaluate conditions top‑down. Once one branch matches, subsequent branches are skipped.

Escape characters and #if conditionals

Leave a Reply

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