Pavel Kononenko
Pavel Kononenko

Reputation: 330

How to render template by dotliquid from custom object?

I need dynamic rendering email template from the specific object from my domain model. I know I can create objects and inherit it from Drop class. Also, I know I can add the specific type to safe type globally, but this doesn't solve the problem. How to render a template from my specific types?

var message = _template.Render(Hash.FromAnonymousObject(new {Item = User}));
public class User { public string Name { get; set;}.....}

Safe type doesn't suitable for this task because in my specific object has nested objects and I need an access to them too.

Template.RegisterSafeType(typeof(User,string[] allowedMembers));

I can inherit my specific types from Drop class of dotliquid assembly but I think it doesn't help me.

Upvotes: 0

Views: 3190

Answers (2)

async
async

Reputation: 1537

RegisterSafeType is the solution if you want to avoid Drop and the alternatives.

Simply register all the relevant types (approximate code):

Template.RegisterSafeType(typeof(User), userAllowedMembers);

Then say User has a property of type Address. You can simply continue the registration with

Template.RegisterSafeType(typeof(Address), addressAllowedMembers);

Upvotes: 1

iMatoria
iMatoria

Reputation: 1448

Dim TemplateContent = CacheHelper.GetFileContentsNonCached("/Path/To/Template/File.htm")

Dim TemplateParsed = DotLiquid.Template.Parse(TemplateContent)
Return TemplateParsed.Render(Hash.FromAnonymousObject(New With {
  .Item = User,
  ......
}))

Above code is in VB.Net but easy to convert to C#. This doesn't require to register any type safe etc.

Upvotes: -1

Related Questions