mirror of https://github.com/abpframework/abp.git
4 changed files with 92 additions and 0 deletions
@ -0,0 +1,9 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.Emailing.Templates |
|||
{ |
|||
public interface ITemplateRender |
|||
{ |
|||
Task<string> RenderAsync(string template, object model = null); |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
using System.Threading.Tasks; |
|||
using Scriban; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Emailing.Templates |
|||
{ |
|||
public class TemplateRender : ITemplateRender, ITransientDependency |
|||
{ |
|||
public async Task<string> RenderAsync(string template, object model = null) |
|||
{ |
|||
var scribanTemplate = Template.Parse(template); |
|||
return await scribanTemplate.RenderAsync(model); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,64 @@ |
|||
using System.Collections.Generic; |
|||
using System.Threading.Tasks; |
|||
using Shouldly; |
|||
using Volo.Abp.Emailing.Templates; |
|||
using Xunit; |
|||
|
|||
namespace Volo.Abp.Emailing |
|||
{ |
|||
public class EmailTemplateRender_Tests : AbpIntegratedTest<AbpEmailingTestModule> |
|||
{ |
|||
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
|||
{ |
|||
options.UseAutofac(); |
|||
} |
|||
|
|||
private readonly ITemplateRender _templateRender; |
|||
|
|||
public EmailTemplateRender_Tests() |
|||
{ |
|||
_templateRender = GetRequiredService<ITemplateRender>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task RenderAsync() |
|||
{ |
|||
var template = "Hello {{email}} {{ for order in orders }}{{ order.id }}:{{ order.name }},{{ end }}"; |
|||
|
|||
var model = new ModelClass |
|||
{ |
|||
Email = "john@abp.io", |
|||
Orders = new List<ModelClass.Order> |
|||
{ |
|||
new ModelClass.Order |
|||
{ |
|||
Id = "1", |
|||
Name = "iphone" |
|||
}, |
|||
new ModelClass.Order |
|||
{ |
|||
Id = "2", |
|||
Name = "ipad" |
|||
} |
|||
} |
|||
}; |
|||
|
|||
var result = await _templateRender.RenderAsync(template, model); |
|||
result.ShouldBe("Hello john@abp.io 1:iphone,2:ipad,"); |
|||
} |
|||
|
|||
public class ModelClass |
|||
{ |
|||
public string Email { get; set; } |
|||
|
|||
public List<Order> Orders { get; set; } |
|||
|
|||
public class Order |
|||
{ |
|||
public string Id { get; set; } |
|||
|
|||
public string Name { get; set; } |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue