tags:

views:

165

answers:

1

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?

A: 

Hey Jake, is .viewname a property? I'm not seeing you setting it in your code, how about you use the following:

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

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!

Robert Reid