我有一个正在尝试使用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,但看起来它们正被替换为空字符串。
有没有人能看到我做错了什么?
发布于 2021-09-09 12:06:53
正如注释中提到的,context应该是一个对象,而不是一个字符串。
var context = new {customer = "mycustomer", tenant = "mytenant"};https://stackoverflow.com/questions/69117651
复制相似问题