如何使用字符串作为力度模板? [英] How to use String as Velocity Template?

查看:52
本文介绍了如何使用字符串作为力度模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从字符串创建Velocity模板的最佳方法是什么?

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

我知道可以通过String或StringReader传递的 Velocity.evaluate 方法,但是我有个更好的办法(例如,创建Template实例的任何优势)).

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).

推荐答案

有一些开销分析模板.如果您的模板很大并且反复使用它,则可以通过预先准备该模板来获得一些性能提升.你可以做这样的事情,

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();

然后,您可以一遍又一遍地调用 template.merge(),而无需每次都对其进行解析.

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

顺便说一句,您可以将String直接传递给 Velocity.evaluate().

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

这篇关于如何使用字符串作为力度模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆