宏未在 NVelocity 中呈现 [英] macros not rendering in NVelocity

查看:62
本文介绍了宏未在 NVelocity 中呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组简单的速度模板.当我尝试使用 NVelocity 进行合并时,其他模板中的宏没有执行.模板内容如下:

I have simple set of velocity templates. When I am trying to merge using NVelocity, the macros from other templates are not executing. The template contents are as follows:

#parse("V2.vm")
#foreach( $customer in $customers)
    Hello $customer.Name!
    #set($a =$customer.getage())
    #age($a)
#end

V2.vm

#macro ( age $a )
    #if($a<18)
        Minor
    #else
        Major
    #end
#end

在合并时,输出为:

Hello User1!

    #age(33)

Hello User2!

    #age(13)

推荐答案

宏不起作用,因为 NVelocity(及其祖先 Velocity)在解析时确定 #age 是指令还是宏,而 #age 宏在运行时被发现,因为它跳转到另一个模板中,因此它作为文本传递.

The macro doesn't work because NVelocity (and its ancestor Velocity) determine if #age is a directive or macro at parse time, while the #age macro gets discovered at runtime as it jumps into the other template, and so it is passed through as text.

要解决这个问题,您需要在解析器解析您的 V1.vm#foreach 之前使宏可用于解析器.显然,您可以通过将宏内联到该文件中来做到这一点,但我假设您打算在其他模板中重用它,这就是您现在将其分开的原因.

To get around this you need to make the macro available to the parser before it parses your V1.vm's #foreach. You can obviously do this by putting the macro inline in that file, but I assume you intend to reuse it in other templates which is why you've got it separate now.

另一种选择是将宏放入宏库中,NVelocity 将自动加载一个(VM_global_library.vm)或自定义一个.如果您在模板目录的根目录下创建一个名为 VM_global_library.vm 的模板,NVelocity 将在解析任何内容之前自动首先加载它,否则创建您自己的宏模板文件并将其注册到 VelocityEngine 带有 velocimacro.library 属性.有关更详细的说明,请参阅 Velocity 文档的属性.

The other option is to put the macro in the macro library, either the one NVelocity will automatically load (VM_global_library.vm) or a custom one. If you create a template named VM_global_library.vm at the root of your templates directory NVelocity will automatically load this first before parsing anything, otherwise create your own macro template file and register it with the VelocityEngine with the velocimacro.library property. See the Velocity documentation for a more detailed explanation of the properties.

我已经包含了一个使用自定义宏库的工作示例:

I've included a working example of using a custom macro library:

class Customer
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int GetAge() { return Age; }
}
class Program
{
    static void Main(string[] args)
    {
        VelocityEngine velocityEngine = new VelocityEngine();
        ExtendedProperties extendedProperties = new ExtendedProperties();
        extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "Templates");
        extendedProperties.SetProperty(RuntimeConstants.VM_LIBRARY, "MyMacros.vm");
        velocityEngine.Init(extendedProperties);

        VelocityContext context = new VelocityContext();
        context.Put("customers", new Customer[] {
            new Customer { Name = "Jack", Age = 33 },
            new Customer { Name = "Jill", Age = 13 }
        });

        using (StringWriter sw = new StringWriter())
        {
            bool result = velocityEngine.MergeTemplate("V1.vm", context, sw);
            Console.WriteLine(sw.ToString());
        }
    }
}

这篇关于宏未在 NVelocity 中呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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