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()
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…