Handlebars.NET .NET Core 中不填写数据的简单示例
Handlebars.NET in .NET Core not filling in data in a simple example
我有一个正在尝试使用 Handlebars.NET 的 .NET 5 应用程序。现在,我只是在做一个非常基本的例子来确保它能正常工作,目前还不能。
我添加了 Nuget Handlebars.NET,但没有添加其他内容。
这是我的模板:
<script id="handlebars-demo" type="text/x-handlebars-template">
<h1>Data Report for {{customer}}</h1>
<h2>Tenant: {{tenant}}</h2>
<p>
This is for testing.
</p>
</script>
这是我的上下文:
var context = "{ \"customer\" : \"mycustomer\", \"tenant\" : \"mytenant\" }";
这是我正在使用的 C# 代码:
var template = Handlebars.Compile( html ); // contains the above template
string finalHtml = template( context );
最终存储在 finalHtml 中的文本如下所示:
<script id="handlebars-demo" type="text/x-handlebars-template">
<h1>Data Report for </h1>
<h2>Tenant: </h2>
<p>
This is for testing.
</p>
</script>
我的目标是将 {{customer}} 替换为 mycustomer 并将 {{template}} 替换为 mytemplate,但它们似乎被替换为空字符串。
谁能看看我做错了什么?
如评论中所述,context
应该是一个对象而不是字符串。
var context = new {customer = "mycustomer", tenant = "mytenant"};
我有一个正在尝试使用 Handlebars.NET 的 .NET 5 应用程序。现在,我只是在做一个非常基本的例子来确保它能正常工作,目前还不能。
我添加了 Nuget Handlebars.NET,但没有添加其他内容。
这是我的模板:
<script id="handlebars-demo" type="text/x-handlebars-template">
<h1>Data Report for {{customer}}</h1>
<h2>Tenant: {{tenant}}</h2>
<p>
This is for testing.
</p>
</script>
这是我的上下文:
var context = "{ \"customer\" : \"mycustomer\", \"tenant\" : \"mytenant\" }";
这是我正在使用的 C# 代码:
var template = Handlebars.Compile( html ); // contains the above template
string finalHtml = template( context );
最终存储在 finalHtml 中的文本如下所示:
<script id="handlebars-demo" type="text/x-handlebars-template">
<h1>Data Report for </h1>
<h2>Tenant: </h2>
<p>
This is for testing.
</p>
</script>
我的目标是将 {{customer}} 替换为 mycustomer 并将 {{template}} 替换为 mytemplate,但它们似乎被替换为空字符串。
谁能看看我做错了什么?
如评论中所述,context
应该是一个对象而不是字符串。
var context = new {customer = "mycustomer", tenant = "mytenant"};