你如何使用 mustache 遍历 c# 中的列表并获取占位符中的每个值
How do you iterate through a list in c# using mustache and get each value in a placeholder
我正在使用 .Net 的 mustache# 来帮助我将值输入到电子邮件应用程序的 html 模板中。我能够使用小胡子占位符来存储单个值,但在遍历列表并将值保存在占位符中时遇到问题。
我在下面提供的代码中的示例列表是一个包含 2 个元素的简单字符串列表。当电子邮件发送并附上 html 模板时,我得到的只是 System.Collections.Generic.List'1[System.String] 两次。
我明白为什么它会出现两次,因为列表中有 2 个元素。我只是不知道如何让它显示每个值。
List<string> namelist = new List<string>();
namelist.Add("Val");
namelist.Add("Jeff");
const string names = "{{#each name}} <h2> Hello, {{name}}</h2> {{/each}}";
HtmlFormatCompiler compilers = new HtmlFormatCompiler();
Generator generator = compilers.Compile(names);
string result = generator.Render(new
{
name = namelist
});
string template = System.IO.File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath
("~/Views/Shared/_HpNotificationEmailTemplate.cshtml"));
string emailBody = string.Format(template,result);
我希望它显示 Val 和 Jeff 的值,但我只得到 System.Collections.Generic.List'1[System.String] 两次。
我不知道,但是 Mustache for C# 模板语法似乎与 javascript 中使用的语法不同。它可能是一个旧的实现,无论如何,解决方案是使用 {{this}} 而不是 {{name}}
List<string> namelist = new List<string>();
namelist.Add("Val");
namelist.Add("Jeff");
const string names = "{{#each name}} <h2> Hello, {{this}}</h2> {{/each}}";
HtmlFormatCompiler compilers = new HtmlFormatCompiler();
Generator generator = compilers.Compile(names);
string result = generator.Render(new
{
name = namelist
});
我正在使用 .Net 的 mustache# 来帮助我将值输入到电子邮件应用程序的 html 模板中。我能够使用小胡子占位符来存储单个值,但在遍历列表并将值保存在占位符中时遇到问题。
我在下面提供的代码中的示例列表是一个包含 2 个元素的简单字符串列表。当电子邮件发送并附上 html 模板时,我得到的只是 System.Collections.Generic.List'1[System.String] 两次。 我明白为什么它会出现两次,因为列表中有 2 个元素。我只是不知道如何让它显示每个值。
List<string> namelist = new List<string>();
namelist.Add("Val");
namelist.Add("Jeff");
const string names = "{{#each name}} <h2> Hello, {{name}}</h2> {{/each}}";
HtmlFormatCompiler compilers = new HtmlFormatCompiler();
Generator generator = compilers.Compile(names);
string result = generator.Render(new
{
name = namelist
});
string template = System.IO.File.ReadAllText(System.Web.HttpContext.Current.Server.MapPath
("~/Views/Shared/_HpNotificationEmailTemplate.cshtml"));
string emailBody = string.Format(template,result);
我希望它显示 Val 和 Jeff 的值,但我只得到 System.Collections.Generic.List'1[System.String] 两次。
我不知道,但是 Mustache for C# 模板语法似乎与 javascript 中使用的语法不同。它可能是一个旧的实现,无论如何,解决方案是使用 {{this}} 而不是 {{name}}
List<string> namelist = new List<string>();
namelist.Add("Val");
namelist.Add("Jeff");
const string names = "{{#each name}} <h2> Hello, {{this}}</h2> {{/each}}";
HtmlFormatCompiler compilers = new HtmlFormatCompiler();
Generator generator = compilers.Compile(names);
string result = generator.Render(new
{
name = namelist
});