Prev Next

API / Apache Velocity Interview questions

Explain the lifecycle of a Velocity template from load to render?

Rendering a template goes through a consistent sequence of stages inside the engine, whether it's the first request or the thousandth.

flowchart TD A[getTemplate name called] --> B{Cached and unmodified?} B -- Yes --> F[Return cached parsed Template/AST] B -- No --> C[ResourceLoader reads raw .vm file] C --> D[Parser (generated by JavaCC) builds an AST of nodes] D --> E[Template object wraps the AST and is cached] E --> F F --> G[template.merge context, writer called] G --> H[Visitor walks the AST] H --> I[References resolved against Context via Uberspector] I --> J[Directives (#if/#foreach/#macro) executed] J --> K[Output streamed to the Writer]

The expensive work, reading the file and building the AST, only happens once per template as long as caching is enabled and the source file's timestamp hasn't changed; every subsequent render reuses that cached AST and just walks it against a fresh Context. This is why keeping file.resource.loader.cache enabled matters so much for throughput: without it, the parse step repeats on every single render.

What is cached after a template is first loaded and parsed, assuming caching is enabled?
During template.merge(), what resolves each $reference against the supplied data?

More Related questions...

What is Apache Velocity? What are the main features of Apache Velocity? What is Velocity Template Language (VTL)? What file extension do Velocity templates typically use? What is the purpose of the VelocityContext? What are Velocity directives? What are the types of loops available in Velocity? How do you use variable references in Velocity templates? How do you apply conditional logic in a Velocity template? Define the #set directive in Velocity? Describe the role of the VelocityEngine class? List the comment syntax options available in VTL? Explain the purpose of the #include directive? What is the purpose of the #parse directive? What is a Velocimacro (#macro directive)? What is the purpose of the #stop directive? What is silent/quiet reference notation ($!) used for? How do you use the #break directive? What are formal vs shorthand references in VTL? How do you render array or list elements with #foreach? Why is Velocity described as a "template engine" rather than a full programming language? Why do we use Velocity instead of embedding logic in JSP? Why should you avoid putting business logic inside Velocity templates? Why doesn't Velocity throw an exception by default for an undefined reference? How does Velocity resolve a variable reference at render time? How is Apache Velocity different from Apache FreeMarker? When should you use #parse over #include? When would you choose a Velocimacro over a Java-side helper method? What happens when a referenced property or method doesn't exist on an object in a template? What is the difference between #set($x = $y) and context.put("x", y) in Java code? Which is better for reusable rendering logic: a Velocimacro or a custom Directive class, and why? How can you optimize Velocity template rendering performance? How do you troubleshoot a "parse error" in a VTL template? Explain the lifecycle of a Velocity template from load to render? What is the difference between local and global (library) Velocimacros? What is strict reference mode in Velocity, and how does it differ from the default lenient mode? How does Velocity integrate with the Apache Struts framework? How does Velocity handle null values inside #if conditions and references? What is the RuntimeInstance in Velocity's architecture? What is the role of the ResourceManager and ResourceLoader in Velocity? How do you configure a custom template ResourceLoader in Velocity? What is the difference in templating philosophy between Apache Velocity and Thymeleaf? How do you escape special characters like $ and # in a Velocity template? What are Velocity Tools (VelocityTools) and why would you use them? How do you handle internationalization (i18n) in Velocity templates? Explain the internal working of Velocimacro resolution and invocation? Explain the internal working of the Velocity template parsing process? What security risks are associated with allowing untrusted users to submit Velocity templates? What is the Uberspector in Velocity and what problem does it solve? Why is Apache Velocity often considered a legacy choice for new Java projects today?
Show more question and Answers...


Comments & Discussions