Skip to content

Commit 3e78b3e

Browse files
Support helpers
1 parent c78a95c commit 3e78b3e

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

generators.new/Generators.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<ItemGroup>
1111
<PackageReference Include="CommandLineParser" Version="2.9.1" />
1212
<PackageReference Include="Handlebars.Net" Version="2.1.6" />
13+
<PackageReference Include="Handlebars.Net.Helpers" Version="2.4.10" />
1314
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
1415
<PackageReference Include="LibGit2Sharp" Version="0.31.0" />
1516
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0" />

generators.new/TemplateRenderer.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json;
22

33
using HandlebarsDotNet;
4+
using HandlebarsDotNet.Helpers;
45
using HandlebarsDotNet.IO;
56

67
using Humanizer;
@@ -11,21 +12,24 @@ namespace Generators;
1112

1213
internal static class TemplateRenderer
1314
{
15+
private static readonly IHandlebars HandlebarsContext = Handlebars.Create();
16+
1417
static TemplateRenderer()
1518
{
16-
Handlebars.RegisterHelper("method_name", (writer, context, parameters) =>
19+
HandlebarsHelpers.Register(HandlebarsContext);
20+
HandlebarsContext.RegisterHelper("method_name", (writer, context, parameters) =>
1721
{
1822
var path = parameters.SelectMany(parameter => parameter as IEnumerable<string> ?? [parameter.ToString()!]);
1923
writer.WriteSafeString(string.Join(" ", path).Dehumanize());
2024
});
21-
Handlebars.Configuration.FormatterProviders.Add(new JsonElementFormatter());
25+
HandlebarsContext.Configuration.FormatterProviders.Add(new JsonElementFormatter());
2226
}
2327

2428
public static string RenderTests(Exercise exercise, TestCase[] testCases) =>
2529
CompileTemplate(exercise)(ToTemplateData(exercise, testCases));
2630

2731
private static HandlebarsTemplate<object, object> CompileTemplate(Exercise exercise) =>
28-
Handlebars.Compile(File.ReadAllText(Paths.TemplateFile(exercise)));
32+
HandlebarsContext.Compile(File.ReadAllText(Paths.TemplateFile(exercise)));
2933

3034
private static Dictionary<string, object> ToTemplateData(Exercise exercise, TestCase[] testCases) =>
3135
new()

0 commit comments

Comments
 (0)