NVelocity - 带有嵌入资源的#parse

时间:2009-08-20 19:36:12

标签: nvelocity

我正在根据嵌入式NVelocity模板生成电子邮件,并希望对动态包含的部分执行某些操作。所以我的嵌入式资源是这样的:

DigestMail.vm _Document.vm _ActionItem.vm _Event.vm

我的电子邮件例程将获取一个对象列表,并将其中的每一个与正确的视图一起传递给DigestMail.vm:

public struct ItemAndView
{
    public string View;
    public object Item;

}

private void GenerateWeeklyEmail(INewItems[] newestItems)
{
    IList<ItemAndView> itemAndViews = new List<ItemAndView>();
    foreach (var item in newestItems)
    {
        itemAndViews.Add(new ItemAndView
        {
            View = string.Format("MyAssembly.MailTemplates._{0}.vm", item.GetType().Name),
            Item = item
        });
    }

    var context = new Dictionary<string, object>();
    context["Recipient"] = _user;
    context["Items"] = itemAndViews;

    string mailBody = _templater.Merge("MyAssembly.MailTemplates.DigestMail.vm", context);
}

在我的DigestMail.vm模板中,我有类似的东西:

#foreach($Item in $Items)
====================================================================
#parse($Item.viewname)
#end

但是当给出这样的嵌入式资源的路径时,它无法#parse。有什么方法可以告诉它解析每个嵌入式模板吗?

2 个答案:

答案 0 :(得分:1)

我会做一个辅助方法,并将InPlace呈现在资源中的视图中,并返回一个将显示的字符串。像这个主题在这里的东西 Template in monorail ViewComponent

答案 1 :(得分:0)

嘿杰克,是.viewname属性?我没有看到你在代码中设置它,你如何使用以下内容:

#foreach($Item in $Items) 
==================================================================== 
$Item.viewname
#end

我不知道为什么你要解析$ Item.viename而不仅仅是使用上面的?我建议这样做,因为我从来不需要解析任何东西!

请参阅我们讨论过模板生成的this帖子。

希望这有帮助!

相关问题