API / Apache Velocity Interview questions
How do you use variable references in Velocity templates?
A reference starts a variable expression with $. Velocity supports two notations: shorthand ($name) and formal (${name}). The formal form is needed whenever the reference is immediately followed by text that could otherwise be read as part of the variable name.
Hello, $user.firstName! Your total is $${cart.total} Filename: report_${date}.pdf
References can also chain method calls and property access, since Velocity uses introspection: $user.getFirstName() and $user.firstName are equivalent, and Velocity tries the getter automatically when you use the property-style shorthand.
If a referenced variable was never placed into the context, Velocity's default (lenient) behavior is to print the reference literally, like $user, rather than throwing an error.
More Related questions...