我尝试将带有对象的数组转储到csv文件。每个对象都是一个带值的行。
{{#unless @last}}; {{/ unless}}嵌套循环中的块被解释为最后一个父元素。
LinqPad有一个代码:
void Main()
{
string template = "{{#each header}}{{this}}{{#unless @last}};{{/unless}}{{/each}}\n{{#each rows}}{{#each this}}{{this}}{{#unless @last}};{{/unless}}{{/each}}\n{{/each}}\n\n\n\n\n\n";
var data = new
{
header = new string [] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
rows = new item [] { new item(), new item(), new item(), new item(), new item(), new item(), new item() }
};
var handlebarsInstance = Handlebars.Create(new HandlebarsConfiguration {TextEncoder = new CsvEncoder()});
Console.WriteLine(handlebarsInstance.Compile(template).Invoke(data));
}
public class CsvEncoder : ITextEncoder
{
public string Encode(string value)
{
return value;
}
}
public class item
{
public string val1 {get; set;} = "A";
public string val2 {get; set;} = "B";
public string val3 {get; set;} = "C";
public string val4 {get; set;} = "D";
public string val5 {get; set;} = "E";
public string val6 {get; set;} = "F";
public string val7 {get; set;} = "G";
}
结果是:
Sun;Mon;Tue;Wed;Thu;Fri;Sat
A;B;C;D;E;F;G;
A;B;C;D;E;F;G;
A;B;C;D;E;F;G;
A;B;C;D;E;F;G;
A;B;C;D;E;F;G;
A;B;C;D;E;F;G;
ABCDEFG
我期待这个结果:
Sun;Mon;Tue;Wed;Thu;Fri;Sat
A;B;C;D;E;F;G
A;B;C;D;E;F;G
A;B;C;D;E;F;G
A;B;C;D;E;F;G
A;B;C;D;E;F;G
A;B;C;D;E;F;G
A;B;C;D;E;F;G
我的车把模板中出现此错误,还是.net中车把解析器中的错误