Replies: 4 comments
|
i tested a simple case with JsonDocument and it seem to work: using System.Text.Json;
using HandlebarsDotNet;
using HandlebarsDotNet.Extension.Json;
var model = JsonDocument.Parse("{\"Key1\": \"Val1\", \"Key2\": \"Val2\"}");
var source = "{{#each this}}{{@key}}={{@value}}\n{{/each}}";
var handlebars = Handlebars.Create();
handlebars.Configuration.UseJson();
var template = handlebars.Compile(source);
var output = template(model);
Console.WriteLine(output);<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<PublishAot>true</PublishAot>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Handlebars.Net" Version="2.1.4" />
<PackageReference Include="Handlebars.Net.Extension.Json" Version="1.0.0" />
</ItemGroup>
</Project>
|
0 replies
|
sadly i cant make it work with a simple dictionary: source =
@"<div class=""entry"">
<h1>{{title}}</h1>
<div class=""body"">
{{body}}
</div>
</div>";
template = Handlebars.Compile(source);
Dictionary<string, string> data = new()
{
{ "title", "my new post" },
{ "body", "this is my post" }
};
var result = template(data);
Console.WriteLine(result); |
0 replies
|
also thankfully it works correctly with custom objects with source generator: using System.Text.Json;
using System.Text.Json.Serialization;
using HandlebarsDotNet;
using HandlebarsDotNet.Extension.Json;
var model = JsonSerializer.SerializeToDocument(
new[]
{
new Model("ivan", "programmer"),
new Model("jane", "cook"),
new Model("joe", "athlete")
},
MyContext.Default.ModelArray);
var source = "{{#each this}}Name: {{name}}\nJob: {{job}}\n\n{{/each}}";
var handlebars = Handlebars.Create();
handlebars.Configuration.UseJson();
var template = handlebars.Compile(source);
var output = template(model);
Console.WriteLine(output);
Console.WriteLine();
public class Model
{
[JsonPropertyName("name")]
public string Name
{
get;
set;
}
[JsonPropertyName("job")]
public string Job
{
get;
set;
}
public Model(string name, string job)
{
Name = name;
Job = job;
}
}
[JsonSerializable(typeof(Model[]))]
public partial class MyContext : JsonSerializerContext
{
} |
0 replies
|
the only problematic part is that i have to do this in the csproj otherwise it will have runtime error on Debug (not release): <PropertyGroup Condition="'$(Configuration)'=='Release'">
<PublishAot>true</PublishAot>
</PropertyGroup>the error: i am not familiar of how the library work but if it is possible to disable dynamic code gen for JsonDocument then i think i wont have to do the csproj thing. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
hi,
is this library supported in a native aot compiled .net application (i am on version .net 8)? thanks.
All reactions