Add template render.

pull/1141/head
maliming 6 years ago
parent d1657d5555
commit e19d603b1b

@ -18,6 +18,10 @@
<None Remove="Volo\Abp\Emailing\Templates\DefaultEmailTemplates\*\*.tpl" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Scriban" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.BackgroundJobs.Abstractions\Volo.Abp.BackgroundJobs.Abstractions.csproj" />
<ProjectReference Include="..\Volo.Abp.Localization\Volo.Abp.Localization.csproj" />

@ -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…
Cancel
Save