mirror of https://github.com/abpframework/abp.git
11 changed files with 150 additions and 20 deletions
@ -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 |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -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!"); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue