API / Apache Velocity Interview questions
Which is better for reusable rendering logic: a Velocimacro or a custom Directive class, and why?
A Velocimacro is the lighter-weight, more common choice: it's defined entirely in VTL (or a macro library file), requires no Java compilation step, and is easy for a template author to read, copy, and adjust. It's the right default for the vast majority of reuse cases, labeled fields, repeated card layouts, common formatting wrappers.
A custom Directive, implemented by extending Velocity's Directive class in Java and registering it with the engine, is heavier to build but gives you full programmatic control over parsing and rendering behavior, including things a Velocimacro can't do, like custom parsing of its own arguments or interacting directly with the rendering context's internal writer.
In practice, custom directives are reserved for genuinely low-level, framework-style needs (Struts and Turbine both ship a handful), while nearly everything else is better served by a Velocimacro, since it keeps the logic visible and editable without a Java build.
More Related questions...