Skip to content

Commit

Permalink
Make TemplateRenderer.RenderAsync arguments optional
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed Dec 19, 2020
1 parent 67d78b0 commit e3e114c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Src/Axuno.TextTemplating/Axuno.TextTemplating.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

The library is a modified version of Volo.Abp.TextTemplating 3.3.1 </Description>
<Copyright>© 2013 - 2020 Volosoft. Open source license with LGPLv3.0</Copyright>
<Version>0.8.0</Version>
<Version>0.8.1</Version>
<Authors>axuno gGmbH</Authors>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageTags>text templating netstandard c#</PackageTags>
Expand Down
2 changes: 1 addition & 1 deletion Src/Axuno.TextTemplating/ITemplateRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface ITemplateRenderer
/// <param name="model">An optional model object that is used in the template.</param>
/// <param name="cultureName">Culture name. Uses the <see cref="CultureInfo.CurrentUICulture"/> if not specified</param>
/// <param name="globalContext">A dictionary which can be used to import global objects to the template</param>
/// <returns></returns>
/// <returns>Returns the rendered text template.</returns>
Task<string> RenderAsync(string templateName,
object? model = null,
string? cultureName = null,
Expand Down
78 changes: 65 additions & 13 deletions Src/Axuno.TextTemplating/TemplateRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,48 @@ namespace Axuno.TextTemplating
/// </remarks>
public class TemplateRenderer : ITemplateRenderer
{
protected readonly ITemplateContentProvider _templateContentProvider;
protected readonly ITemplateDefinitionManager _templateDefinitionManager;
protected readonly IStringLocalizerFactory _stringLocalizerFactory;

/// <summary>
/// Gets the <see cref="ITemplateContentProvider"/>.
/// </summary>
protected readonly ITemplateContentProvider TemplateContentProvider;
/// <summary>
/// Gets the <see cref="ITemplateDefinitionProvider"/>.
/// </summary>
protected readonly ITemplateDefinitionManager TemplateDefinitionManager;
/// <summary>
/// Gets the <see cref="IStringLocalizerFactory"/>.
/// </summary>
protected readonly IStringLocalizerFactory StringLocalizerFactory;

/// <summary>
/// Creates a new instance of a <see cref="TemplateRenderer"/> class.
/// </summary>
/// <param name="templateContentProvider"></param>
/// <param name="templateDefinitionManager"></param>
/// <param name="stringLocalizerFactory"></param>
public TemplateRenderer(
ITemplateContentProvider templateContentProvider,
ITemplateDefinitionManager templateDefinitionManager,
IStringLocalizerFactory stringLocalizerFactory)
{
_templateContentProvider = templateContentProvider;
_templateDefinitionManager = templateDefinitionManager;
_stringLocalizerFactory = stringLocalizerFactory;
TemplateContentProvider = templateContentProvider;
TemplateDefinitionManager = templateDefinitionManager;
StringLocalizerFactory = stringLocalizerFactory;
}

/// <summary>
/// Renders a text template.
/// </summary>
/// <param name="templateName">The template name</param>
/// <param name="model">An optional model object that is used in the template.</param>
/// <param name="cultureName">Culture name. Uses the <see cref="CultureInfo.CurrentUICulture"/> if not specified</param>
/// <param name="globalContext">A dictionary which can be used to import global objects to the template</param>
/// <returns>Returns the rendered text template.</returns>
public virtual async Task<string> RenderAsync(
string templateName,
object? model,
string? cultureName,
Dictionary<string, object>? globalContext)
object? model = null,
string? cultureName = null,
Dictionary<string, object>? globalContext = null)
{
globalContext ??= new Dictionary<string, object>();

Expand All @@ -58,12 +81,19 @@ public virtual async Task<string> RenderAsync(
}
}

/// <summary>
/// Renders a text template.
/// </summary>
/// <param name="templateName">The template name</param>
/// <param name="model">An optional model object that is used in the template.</param>
/// <param name="globalContext">A dictionary which can be used to import global objects to the template</param>
/// <returns>Returns the rendered text template.</returns>
protected virtual async Task<string> RenderInternalAsync(
string templateName,
Dictionary<string, object> globalContext,
object? model = null)
{
var templateDefinition = _templateDefinitionManager.Get(templateName);
var templateDefinition = TemplateDefinitionManager.Get(templateName);
if (templateDefinition is null) throw new Exception($"Template name '{templateName}' not found.");

var renderedContent = await RenderSingleTemplateAsync(
Expand All @@ -84,12 +114,19 @@ protected virtual async Task<string> RenderInternalAsync(
return renderedContent;
}

/// <summary>
/// Renders a text template.
/// </summary>
/// <param name="templateDefinition">The template definition</param>
/// <param name="globalContext">A dictionary which can be used to import global objects to the template</param>
/// <param name="model">An optional model object that is used in the template.</param>
/// <returns>Returns the rendered text template.</returns>
protected virtual async Task<string> RenderSingleTemplateAsync(
TemplateDefinition templateDefinition,
Dictionary<string, object> globalContext,
object? model = null)
{
var rawTemplateContent = await _templateContentProvider
var rawTemplateContent = await TemplateContentProvider
.GetContentAsync(
templateDefinition
);
Expand All @@ -102,6 +139,14 @@ protected virtual async Task<string> RenderSingleTemplateAsync(
);
}

/// <summary>
/// Renders a text template with Scriban.
/// </summary>
/// <param name="templateDefinition">The template definition</param>
/// <param name="templateContent">The template content</param>
/// <param name="globalContext">A dictionary which can be used to import global objects to the template</param>
/// <param name="model">An optional model object that is used in the template.</param>
/// <returns>Returns the rendered text template.</returns>
protected virtual async Task<string> RenderTemplateContentWithScribanAsync(
TemplateDefinition templateDefinition,
string? templateContent,
Expand All @@ -119,6 +164,13 @@ protected virtual async Task<string> RenderTemplateContentWithScribanAsync(
.RenderAsync(context);
}

/// <summary>
/// Creates a new Scriban <see cref="TemplateContext"/>.
/// </summary>
/// <param name="templateDefinition">The <see cref="TemplateDefinition"/>.</param>
/// <param name="globalContext">The global context.</param>
/// <param name="model">The model to render the template.</param>
/// <returns></returns>
protected virtual TemplateContext CreateScribanTemplateContext(
TemplateDefinition templateDefinition,
Dictionary<string, object> globalContext,
Expand All @@ -145,7 +197,7 @@ protected virtual TemplateContext CreateScribanTemplateContext(

private IStringLocalizer GetLocalizer(TemplateDefinition templateDefinition)
{
return _stringLocalizerFactory.Create(templateDefinition.LocalizationResource ?? typeof(object));
return StringLocalizerFactory.Create(templateDefinition.LocalizationResource ?? typeof(object));
}
}
}

0 comments on commit e3e114c

Please sign in to comment.