Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use String as Velocity Template?

Tags:

java

velocity

What is the best way to create Velocity Template from a String?

I'm aware of Velocity.evaluate method where I can pass String or StringReader, but I'm curios is there a better way to do it (e.g. any advantage of creating an instance of Template).

like image 345
tomsame Avatar asked Sep 16 '09 11:09

tomsame


People also ask

How do you write a Velocity template?

3.2 Create a Velocity Template Writer Initialize the Velocity engine. Read the template. Put the data model in a context object. Merge the template with context data and render the view.

What is a Velocity template?

Velocity is a server-side template language used by Confluence to render page content. Velocity allows Java objects to be called alongside standard HTML. If you are are writing a user macro or developing a plugin you may need to modify Velocity content.

How do you use Velocity in HTML?

In the code you posted: there 2 ways to run velocity: running the main (but this is not what you do when you build your project) running ant form but since you don't fill any velocity context: velocity won't be able to replace the variable in your template (i.e. the generated html won't contains dynamic data)


2 Answers

There is some overhead parsing template. You might see some performance gain by pre-parsing the template if your template is large and you use it repeatedly. You can do something like this,

RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices(); StringReader reader = new StringReader(bufferForYourTemplate); Template template = new Template(); template.setRuntimeServices(runtimeServices);  /*  * The following line works for Velocity version up to 1.7  * For version 2, replace "Template name" with the variable, template  */ template.setData(runtimeServices.parse(reader, "Template name")));  template.initDocument(); 

Then you can call template.merge() over and over again without parsing it everytime.

BTW, you can pass String directly to Velocity.evaluate().

like image 157
ZZ Coder Avatar answered Sep 22 '22 10:09

ZZ Coder


The above sample code is working for me. It uses Velocity version 1.7 and log4j.

private static void velocityWithStringTemplateExample() {     // Initialize the engine.     VelocityEngine engine = new VelocityEngine();     engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");     engine.setProperty("runtime.log.logsystem.log4j.logger", LOGGER.getName());     engine.setProperty(Velocity.RESOURCE_LOADER, "string");     engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());     engine.addProperty("string.resource.loader.repository.static", "false");     //  engine.addProperty("string.resource.loader.modificationCheckInterval", "1");     engine.init();      // Initialize my template repository. You can replace the "Hello $w" with your String.     StringResourceRepository repo = (StringResourceRepository) engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);     repo.putStringResource("woogie2", "Hello $w");      // Set parameters for my template.     VelocityContext context = new VelocityContext();     context.put("w", "world!");      // Get and merge the template with my parameters.     Template template = engine.getTemplate("woogie2");     StringWriter writer = new StringWriter();     template.merge(context, writer);      // Show the result.     System.out.println(writer.toString()); } 

A similar so question.

like image 29
Georgios Syngouroglou Avatar answered Sep 22 '22 10:09

Georgios Syngouroglou