Reputation: 21
I am using mustache# for .Net and to help me get values into a html template for an email application. I am able to get mustache placeholders to store single values but having issues iterating through a a list and saving the values in a placeholder.
My example list in the code I provide below is a simple string List with 2 elements. When the email sends and the html template is attached All I am getting for this is System.Collections.Generic.List'1[System.String] twice. I understand why its showing up twice because of there being 2 elements in the list. I just don't know how to have it show each value instead.
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);
I am expecting it to show the values Val and Jeff but I am just getting System.Collections.Generic.List'1[System.String] twice.
Upvotes: 0
Views: 3064
Reputation: 390
I don't know but the the Mustache for C# template syntax does not seem to be the same as that used in javascript. It probably an old implementation, anyway, the solution is to use {{this}} instead of {{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
});
Upvotes: 1