速度模板元数据

Velocity template metadata

Apache Velocity 是否包含向模板添加元数据的机制?

我正在尝试向我的模板添加一些额外信息(例如,类型和描述性名称),然后读取这些信息以编程方式按类型对模板进行分组,并使用它们在 UI 上列出模板描述性名称。

我试过使用文字 #[[...]]# 块(并解析它们)和 #set 指令,但两者都有问题。它们是 hacky(需要对模板进行一些解析)并且远非优雅。

嗯,我不知道有什么 built-in 可以做到这一点。不过,为了避免在第一遍处理整个模板,一个技巧是在该遍期间有条件地抛出异常(下面的 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
-----------