Added template content provider and template renderer.

pull/3792/head
Halil İbrahim Kalkan 6 years ago
parent 9060b695ea
commit 67dbc9b453

@ -33,5 +33,28 @@ namespace Volo.Abp.TextTemplating
options.DefinitionProviders.AddIfNotContains(definitionProviders);
});
}
public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
//TODO: Consider to move to the TemplateContentProvider and invoke lazy (with making it singleton)
using (var scope = context.ServiceProvider.CreateScope())
{
var templateDefinitionManager = scope.ServiceProvider
.GetRequiredService<ITemplateDefinitionManager>();
foreach (var templateDefinition in templateDefinitionManager.GetAll())
{
var contributorInitializationContext = new TemplateContributorInitializationContext(
templateDefinition,
scope.ServiceProvider
);
foreach (var contributor in templateDefinition.Contributors)
{
contributor.Initialize(contributorInitializationContext);
}
}
}
}
}
}

@ -0,0 +1,9 @@
using System.Threading.Tasks;
namespace Volo.Abp.TextTemplating
{
public interface ITemplateContentProvider
{
Task<string> GetContentOrNullAsync(string templateName, string cultureName);
}
}

@ -1,9 +1,11 @@
namespace Volo.Abp.TextTemplating
using JetBrains.Annotations;
namespace Volo.Abp.TextTemplating
{
public interface ITemplateContributor
{
void Initialize(TemplateContributorInitializationContext context);
string GetOrNull(string cultureName);
string GetOrNull([CanBeNull] string cultureName);
}
}

@ -0,0 +1,14 @@
using System.Linq;
using System.Threading.Tasks;
using JetBrains.Annotations;
namespace Volo.Abp.TextTemplating
{
public interface ITemplateRenderer
{
Task<string> RenderAsync(
[NotNull] string templateName,
[CanBeNull] string cultureName = null
);
}
}

@ -0,0 +1,35 @@
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.TextTemplating
{
public class TemplateContentProvider : ITemplateContentProvider, ITransientDependency
{
private readonly ITemplateDefinitionManager _templateDefinitionManager;
public TemplateContentProvider(
ITemplateDefinitionManager templateDefinitionManager
)
{
_templateDefinitionManager = templateDefinitionManager;
}
public async Task<string> GetContentOrNullAsync(string templateName, string cultureName)
{
var template = _templateDefinitionManager.Get(templateName);
foreach (var contributor in template.Contributors)
{
var templateString = contributor.GetOrNull(cultureName);
if (templateString != null)
{
return templateString;
}
}
throw new AbpException(
$"None of the template contributors could get the content for the template '{templateName}'"
);
}
}
}

@ -1,22 +1,9 @@
using System.Collections.Generic;
using System.Linq;
namespace Volo.Abp.TextTemplating
{
public class TemplateContributorList : List<ITemplateContributor>
{
public string GetOrNull(string cultureName)
{
foreach (var contributor in this.AsQueryable().Reverse())
{
var templateString = contributor.GetOrNull(cultureName);
if (templateString != null)
{
return templateString;
}
}
return null;
}
}
}

@ -0,0 +1,30 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.TextTemplating
{
public class TemplateRenderer : ITemplateRenderer, ITransientDependency
{
private readonly ITemplateContentProvider _templateContentProvider;
public TemplateRenderer(
ITemplateContentProvider templateContentProvider
)
{
_templateContentProvider = templateContentProvider;
}
public virtual async Task<string> RenderAsync(
[NotNull] string templateName,
[CanBeNull] string cultureName = null)
{
Check.NotNullOrWhiteSpace(templateName, nameof(templateName));
return await _templateContentProvider.GetContentOrNullAsync(
templateName,
cultureName
);
}
}
}

@ -15,8 +15,8 @@ namespace Volo.Abp.TextTemplating
[Fact]
public void Should_Retrieve_Template_Definition_By_Name()
{
var definition = _templateDefinitionManager.Get(TestTemplates.TestTemplate1);
definition.Name.ShouldBe(TestTemplates.TestTemplate1);
var definition = _templateDefinitionManager.Get(TestTemplates.WelcomeEmail);
definition.Name.ShouldBe(TestTemplates.WelcomeEmail);
}
[Fact]

@ -0,0 +1,23 @@
using System.Threading.Tasks;
using Shouldly;
using Xunit;
namespace Volo.Abp.TextTemplating
{
public class TemplateRenderer_Tests : AbpTextTemplatingTestBase
{
private readonly ITemplateRenderer _templateRenderer;
public TemplateRenderer_Tests()
{
_templateRenderer = GetRequiredService<ITemplateRenderer>();
}
[Fact]
public async Task Should_Get_Rendered_Non_Localized_Template_Content()
{
var content = await _templateRenderer.RenderAsync(TestTemplates.ForgotPasswordEmail);
content.ShouldBe("Please click to the following link to get an email to reset your password!");
}
}
}

@ -6,10 +6,16 @@
{
context.Add(
new TemplateDefinition(
TestTemplates.TestTemplate1
TestTemplates.WelcomeEmail
).AddVirtualFiles("/SampleTemplates/WelcomeEmail")
);
context.Add(
new TemplateDefinition(
TestTemplates.ForgotPasswordEmail
).AddVirtualFiles("/SampleTemplates/ForgotPasswordEmail.tpl")
);
context.Add(new TemplateDefinition(
TestTemplates.TestTemplateLayout1
));

@ -2,7 +2,8 @@
{
public static class TestTemplates
{
public const string TestTemplate1 = "TestTemplate1";
public const string WelcomeEmail = "WelcomeEmail";
public const string ForgotPasswordEmail = "ForgotPasswordEmail";
public const string TestTemplateLayout1 = "TestTemplateLayout1";
}
}
Loading…
Cancel
Save