0

I have a list of strings in the memory. I want to load all of them as velocity templates during the application startup. How do I do it ?

I know how to create template from a string from here: How to use String as Velocity Template?

But I want to know how to load all template strings in one shot

Community
  • 1
  • 1
Dunxton
  • 408
  • 1
  • 8
  • 21

1 Answers1

2

Loop over the collection of strings and initialize them all--not sure what else you're looking for.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • I have 2 questions 1. I am not sure which piece of the code mentioned in that link exactly initializes the template. DO i have to create a new template everytime and call `template.initDocument();` to initialize the template ? or is calling `runtimeServices.parse(reader, "Template name"));` sufficient ? 2. After loading, how do I fetch a particular template ? IS it like `velocityEngine.getTemplate("Template name")` – Dunxton Sep 29 '11 at 12:00
  • You'd need to keep references to the compiled templates. IIRC `getTemplate` is for retrieving templates using normal template resource management (classpath, filesystem, etc.) The code chunk shown is what initializes the template, although I don't know if it's *all* necessary, or if there are shorter ways. Not really sure what you're trying to do, so it's difficult to provide advice beyond the specific question you're asking--right now it sounds to me like you're doing it wrong, though :) – Dave Newton Sep 29 '11 at 12:14
  • In freemarker, I can initialize a StringTemplateLoader and add all my templates there. And later I can access the cached templates by their name. I was hoping to find something similar in velocity but seems otherwise :( – Dunxton Sep 29 '11 at 12:42
  • Not that I'm aware of. FreeMarker has better error reporting, too. – Dave Newton Sep 29 '11 at 12:45