我有一个。net 5应用程序,试图使用Handlebars.NET。现在,我只是在做一个非常基本的例子来确保它能工作,而它目前还不能。
我已经添加了Nuget把手。. 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"};
请参考文档- https://github.com/Handlebars-Net/Handlebars.Net