Skip to content

Commit 0c57757

Browse files
committed
v0.7.2: Added .Net 5.0 support and demo program
1 parent 5d0c3c0 commit 0c57757

25 files changed

Lines changed: 943 additions & 3 deletions

Src/Axuno.TextTemplating/Axuno.TextTemplating.csproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
4+
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
55
<Nullable>enable</Nullable>
66
<Description>Text templating is used to dynamically render contents based on a template and a model.
77

@@ -12,7 +12,7 @@
1212

1313
The library is a modified version of Volo.Abp.TextTemplating 3.3.1 </Description>
1414
<Copyright>© 2013 - 2020 Volosoft. Open source license with LGPLv3.0</Copyright>
15-
<Version>0.7.1</Version>
15+
<Version>0.7.2</Version>
1616
<Authors>axuno gGmbH</Authors>
1717
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
1818
<PackageTags>text templating netstandard c#</PackageTags>
@@ -25,7 +25,8 @@ The library is a modified version of Volo.Abp.TextTemplating 3.3.1 </Description
2525
<RepositoryType>Git</RepositoryType>
2626
<PackageIcon>TextTemplating.png</PackageIcon>
2727
<PackageProjectUrl>https://github.com/axuno/Axuno.TextTemplating</PackageProjectUrl>
28-
<PackageReleaseNotes>Initial release</PackageReleaseNotes>
28+
<PackageReleaseNotes>Initial release with .Net 5.0 framework added</PackageReleaseNotes>
29+
<OutputType>Library</OutputType>
2930
</PropertyGroup>
3031

3132
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -40,6 +41,7 @@ The library is a modified version of Volo.Abp.TextTemplating 3.3.1 </Description
4041
<Deterministic>true</Deterministic>
4142
<PublishRepositoryUrl>true</PublishRepositoryUrl>
4243
<EmbedUntrackedSources>true</EmbedUntrackedSources>
44+
<IncludeSymbols>true</IncludeSymbols>
4345
</PropertyGroup>
4446

4547
<ItemGroup>

Src/TextTemplating.sln

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30413.136
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TextTemplatingDemo", "TextTemplatingDemo\TextTemplatingDemo.csproj", "{E0B5186C-B914-4B7E-B225-15735FEA5FCD}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Axuno.TextTemplating", "Axuno.TextTemplating\Axuno.TextTemplating.csproj", "{80BB24DA-D843-4A91-A940-774F8975DABE}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{E0B5186C-B914-4B7E-B225-15735FEA5FCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{E0B5186C-B914-4B7E-B225-15735FEA5FCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{E0B5186C-B914-4B7E-B225-15735FEA5FCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{E0B5186C-B914-4B7E-B225-15735FEA5FCD}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{80BB24DA-D843-4A91-A940-774F8975DABE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{80BB24DA-D843-4A91-A940-774F8975DABE}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{80BB24DA-D843-4A91-A940-774F8975DABE}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{80BB24DA-D843-4A91-A940-774F8975DABE}.Release|Any CPU.Build.0 = Release|Any CPU
24+
{F0F3F97C-63EB-4947-BDAB-0C0B947AEB50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
25+
{F0F3F97C-63EB-4947-BDAB-0C0B947AEB50}.Debug|Any CPU.Build.0 = Debug|Any CPU
26+
{F0F3F97C-63EB-4947-BDAB-0C0B947AEB50}.Release|Any CPU.ActiveCfg = Release|Any CPU
27+
{F0F3F97C-63EB-4947-BDAB-0C0B947AEB50}.Release|Any CPU.Build.0 = Release|Any CPU
28+
EndGlobalSection
29+
GlobalSection(SolutionProperties) = preSolution
30+
HideSolutionNode = FALSE
31+
EndGlobalSection
32+
GlobalSection(ExtensibilityGlobals) = postSolution
33+
SolutionGuid = {0C263EE1-958C-482E-990E-012E01DE33FB}
34+
EndGlobalSection
35+
EndGlobal
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using Axuno.TextTemplating;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using TextTemplatingDemo.Localization;
6+
7+
namespace TextTemplatingDemo
8+
{
9+
// inject as transient
10+
public class DemoTemplateDefinitionProvider : TemplateDefinitionProvider
11+
{
12+
public override void Define(ITemplateDefinitionContext context)
13+
{
14+
context.Add(
15+
new TemplateDefinition("Hello", typeof(DemoResource), defaultCultureName: "") //template name: "Hello"
16+
.WithVirtualFilePath(
17+
"/Demos/Hello/Hello.tpl", //template content path
18+
isInlineLocalized: true
19+
)
20+
);
21+
22+
context.Add(
23+
new TemplateDefinition(
24+
name: "PasswordReset",
25+
localizationResource: typeof(DemoResource)
26+
).WithVirtualFilePath(
27+
"/Demos/PasswordReset/PasswordReset.tpl", //template content path
28+
isInlineLocalized: true
29+
)
30+
);
31+
32+
context.Add(
33+
new TemplateDefinition(
34+
name: "WelcomeEmail",
35+
defaultCultureName: "en",
36+
layout: "EmailLayout" //Set the LAYOUT
37+
).WithVirtualFilePath(
38+
"/Demos/WelcomeEmail/Templates", //template content folder
39+
isInlineLocalized: false
40+
)
41+
);
42+
43+
context.Add(
44+
new TemplateDefinition(
45+
"EmailLayout",
46+
isLayout: true
47+
).WithVirtualFilePath(
48+
"/Demos/EmailLayout/EmailLayout.tpl", //template content path
49+
isInlineLocalized: true
50+
)
51+
);
52+
53+
context.Add(
54+
new TemplateDefinition("GlobalContextUsage")
55+
.WithVirtualFilePath(
56+
"/Demos/GlobalContext/GlobalContextUsage.tpl",
57+
isInlineLocalized: true
58+
)
59+
);
60+
61+
context.Add(
62+
new TemplateDefinition("Custom")
63+
.WithVirtualFilePath(
64+
"/Demos/CustomRenderer/Custom.tpl",
65+
isInlineLocalized: true
66+
)
67+
);
68+
}
69+
}
70+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Hello {{ model.FirstName | string.append " (G.)" }} {{ model.LastName }}
2+
{{ NotExistingInModel }}
3+
{{ MySimpleFunction }}
4+
Result of custom function: {{ MyCustomFunction 123 456 789 }}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Axuno.TextTemplating;
8+
using TextTemplateDemo.Demos.Hello;
9+
10+
namespace TextTemplatingDemo.Demos.CustomRenderer
11+
{
12+
class CustomRendererDemo
13+
{
14+
private readonly ITemplateRenderer _templateRenderer;
15+
16+
public CustomRendererDemo(ServiceProvider? services)
17+
{
18+
if (services is null) throw new ArgumentNullException(nameof(services));
19+
_templateRenderer = services.GetServices<ITemplateRenderer>().First(s => s.GetType() == typeof(CustomTemplateRenderer));
20+
}
21+
22+
public async Task RunAsync()
23+
{
24+
var result = await _templateRenderer.RenderAsync(
25+
"Custom", //the template name
26+
new
27+
{
28+
FirstName = "John",
29+
LastName = "Specimen"
30+
}
31+
);
32+
33+
Console.WriteLine(result);
34+
}
35+
}
36+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Axuno.TextTemplating;
5+
using Microsoft.Extensions.Localization;
6+
using Scriban;
7+
using Scriban.Parsing;
8+
using Scriban.Runtime;
9+
using Scriban.Syntax;
10+
11+
namespace TextTemplatingDemo.Demos.CustomRenderer
12+
{
13+
/// <summary>
14+
/// Custom template renderer.
15+
/// </summary>
16+
/// <remarks>
17+
/// Inject as transient.
18+
/// </remarks>
19+
public class CustomTemplateRenderer : TemplateRenderer
20+
{
21+
public CustomTemplateRenderer(
22+
ITemplateContentProvider templateContentProvider,
23+
ITemplateDefinitionManager templateDefinitionManager,
24+
IStringLocalizerFactory stringLocalizerFactory) : base(templateContentProvider, templateDefinitionManager, stringLocalizerFactory)
25+
{ }
26+
27+
/*
28+
protected override async Task<string> RenderTemplateContentWithScribanAsync(
29+
TemplateDefinition templateDefinition,
30+
string? templateContent,
31+
Dictionary<string, object> globalContext,
32+
object? model = null)
33+
{
34+
var context = CreateScribanTemplateContext(
35+
templateDefinition,
36+
globalContext,
37+
model
38+
);
39+
40+
return await Template
41+
.Parse(templateContent)
42+
.RenderAsync(context);
43+
}*/
44+
45+
protected override TemplateContext CreateScribanTemplateContext(
46+
TemplateDefinition templateDefinition,
47+
Dictionary<string, object> globalContext,
48+
object? model = null)
49+
{
50+
var baseContext = base.CreateScribanTemplateContext(templateDefinition, globalContext, model);
51+
52+
// The ScriptObject is already registered as global
53+
baseContext.StrictVariables = true; // Note: has no effect for child members
54+
baseContext.MemberRenamer = member => member.Name; // Use .NET object without changing the cases
55+
// will be called if the member could not be found:
56+
baseContext.TryGetMember = (TemplateContext context, SourceSpan span, object target, string member,
57+
out object value) =>
58+
{
59+
value = $"## Member '{member}' not found ##";
60+
return true;
61+
};
62+
// will be called if the variable could not be found
63+
baseContext.
64+
TryGetVariable =
65+
(TemplateContext context, SourceSpan span, ScriptVariable variable, out object value) =>
66+
{
67+
value = value = $"## Variable '{variable}' not found ##";
68+
return true;
69+
};
70+
71+
baseContext.BuiltinObject.SetValue(nameof(MyCustomFunction), new MyCustomFunction(), true);
72+
baseContext.BuiltinObject.Import("MySimpleFunction", new Func<string>(() => "Text from MySimpleFunction"));
73+
74+
return baseContext;
75+
}
76+
}
77+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using Scriban;
4+
using Scriban.Runtime;
5+
using Scriban.Syntax;
6+
7+
namespace TextTemplatingDemo.Demos.CustomRenderer
8+
{
9+
public class MyCustomFunction : IScriptCustomFunction
10+
{
11+
public object Invoke(TemplateContext context, ScriptNode callerContext, ScriptArray arguments,
12+
ScriptBlockStatement blockStatement)
13+
{
14+
return GetString(arguments);
15+
}
16+
17+
public ValueTask<object> InvokeAsync(TemplateContext context, ScriptNode callerContext, ScriptArray arguments,
18+
ScriptBlockStatement blockStatement)
19+
{
20+
return new ValueTask<object>(GetString(arguments));
21+
}
22+
23+
private static string GetString(ScriptArray arguments)
24+
{
25+
if (arguments.Count == 0)
26+
{
27+
return "*Nothing to calculate*";
28+
}
29+
30+
var name = arguments[0];
31+
if (name == null || string.IsNullOrWhiteSpace(name.ToString()))
32+
{
33+
return string.Empty;
34+
}
35+
36+
var args = arguments.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())).Select(x => int.Parse(x.ToString() ?? "0")).ToArray();
37+
38+
return args.Any() ? string.Join(" + ", args) + " = " + args.Sum() : "*Nothing to calculate*";
39+
}
40+
}
41+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
3+
<head><meta charset="utf-8" /></head>
4+
<body>
5+
{{content}}
6+
</body>
7+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A global object value: {{myGlobalObject}}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Axuno.TextTemplating;
5+
6+
7+
namespace TextTemplateDemo.Demos.GlobalContext
8+
{
9+
public class GlobalContextUsageDemo
10+
{
11+
private readonly ITemplateRenderer _templateRenderer;
12+
13+
public GlobalContextUsageDemo(ITemplateRenderer templateRenderer)
14+
{
15+
_templateRenderer = templateRenderer;
16+
}
17+
18+
public async Task RunAsync()
19+
{
20+
var result = await _templateRenderer.RenderAsync(
21+
"GlobalContextUsage",
22+
globalContext: new Dictionary<string, object>
23+
{
24+
{"myGlobalObject", "TEST VALUE"}
25+
}
26+
);
27+
28+
Console.WriteLine(result);
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)