嗯,我不知道有什么内置的东西可以做到这一点。不过,为了避免在第一次通过时处理整个模板,一个技巧是在该次通过期间有条件地抛出异常(下面的MetadataFinished),但不是正常执行。
显然,这仍然需要预先编译整个模板,尽管这在执行时应该会派上用场。
例如
import org.apache.commons.io.output.NullWriter;
public class Metadata {
private Map<String, Template> byKey = new LinkedHashMap<>();
private Template currentTemplate;
/** Callback from .vm */
public void set(String key) throws MetadataFinished {
// Only do this in addTemplate()
if (currentTemplate != null) {
byKey.put(key, currentTemplate);
throw new MetadataFinished();
}
}
public void addTemplate(Template template) {
currentTemplate = template;
try {
Context context = new VelocityContext();
context.put("metadata", this);
template.merge(context, new NullWriter());
} catch (MetadataFinished ex) {
// Ignored
} finally {
currentTemplate = null;
}
}
public void execute(String key) {
Template template = byKey.get(key);
Context context = new VelocityContext();
PrintWriter pw = new PrintWriter(System.out);
template.merge(context, pw);
pw.flush();
}
// Extends Error to avoid Velocity adding a wrapping MethodInvocationException
private static class MetadataFinished extends Error {
}
public static void main(String[] args) {
Metadata metadata = new Metadata();
VelocityEngine engine = new VelocityEngine();
engine.setProperty("file.resource.loader.path", "/temp");
engine.init();
String[] fileNames = { "one.vm", "two.vm" };
for (String fileName : fileNames) {
Template template = engine.getTemplate(fileName);
metadata.addTemplate(template);
}
metadata.execute("vm1");
metadata.execute("vm2");
}
}
然后在one.vm:
$!metadata.set("vm1")##
-----------
This is VM1
-----------
## 有点难看——它只是为了停止输出一个空行。如果可读性很重要,则可以使用宏使这更简洁:
#metadata("vm2")
-----------
This is VM2
-----------
该宏可以在全局VM_global_library.vm中定义:
#macro( metadata $key )
$!metadata.set($key)#end
仅供参考,输出如预期:
-----------
This is VM1
-----------
-----------
This is VM2
-----------