0 votes

Could I use a delegate as a data provider for email template? Before I start processing the template I couldn't know which placeholders are there.

by

1 Answer

+1 vote

Although it's not directly possible you can use IDictionary for that.

internal class MyProvider : Dictionary<string, string>, IDictionary
{
    bool IDictionary.Contains(object name)
    {
        return true;
    }

    public object this[object key]
    {
        get { return "Value for " + key; }
        set {  }
    }
}

Assert.AreEqual("Value for TheKey", Template.Create("[TheKey]").DataFrom(new MyProvider()).Render());

Converting this to support lambda is rather trivial.

I know this is a bit of a hack, and I think we'll expose ITemplateDataProvider interface in the next release:

internal class CustomProvider : ITemplateDataProvider
{
    public object GetValue(string key)
    {
        return "Value for " + key; }

    public bool HasValue(string name)
    {
        return  true;
    }
}

Assert.AreEqual("Value for TheKey", Template.Create("[TheKey]").DataFrom(new CustomProvider()).Render());
by (301k points)
...