NVelocity——带有嵌入式资源的#parse
我正在根据嵌入式 NVelocity 模板生成电子邮件,并希望对动态包含的部分进行一些操作。所以我的嵌入式资源是这样的:
DigestMail.vm _文档.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给定像这样的嵌入式资源的路径。有什么方法可以让它解析每个嵌入的模板吗?
I'm generating emails based off embedded NVelocity templates and would like to do something with dynamically included sections. So my embedded resources are something like this:
DigestMail.vm
_Document.vm
_ActionItem.vm
_Event.vm
My email routine will get a list of objects and will pass each of these along with the proper view to 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);
}
And in my DigestMail.vm template I've got something like this:
#foreach($Item in $Items)
====================================================================
#parse($Item.viewname)
#end
But it's unable to #parse when given the path to an embedded resource like this. Is there any way I can tell it to parse each of these embedded templates?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嘿杰克,.viewname 是一个属性吗?我没有看到您在代码中设置它,您使用以下内容怎么样:
我不知道为什么您要解析 $Item.viename 而不是仅使用上面的内容?我建议这样做是因为我从来不需要解析任何东西!
请参阅这篇文章,我们在其中讨论了模板的生成。
希望这有帮助!
Hey Jake, is .viewname a property? I'm not seeing you setting it in your code, how about you use the following:
I don't know why you're parsing the $Item.viename rather than just using the above? I'm suggesting this as I've just never needed to parse anything!
Please refer to this post where we've discussed the generation of templates.
Hope this helps!