在 StringTemplate4 备忘单(http://www.antlr.org/wiki/display/ST/StringTemplate+cheat+sheet)中,它提到执行迭代
<attribute:{anonymous-template}>
Apply an anonymous template to each element of attribute. The iterated `it` attribute is set automatically.
我试过下面的代码:
List<TextParseTests.TestModel> data = new List<TextParseTests.TestModel>();
for (int i = 0; i < 10; i++)
{
TextParseTests.TestModel model2 = new TextParseTests.TestModel();
model2.Name = i.ToString();
data.Add(model2);
}
string template = @"TestTemplate|| <List:{ [DataList <it.Name>] }> [END]";
Template t = new Template(template);
t.Add("List", data.ToArray());
var result = t.Render();
sb.AppendLine(result);
更新 1
下面是 TestModel 数据结构和相关类。我正在使用这些只是
public class ContactDetailsTest
{
public string Email { get; set; }
public string Address1 { get; set; }
}
public class TestModel
{
public string Name { get; set; }
public string Surname { get; set; }
public ContactDetailsTest ContactDetails { get; set; }
public TestModel()
{
this.ContactDetails = new ContactDetailsTest();
}
}
然而最终的结果是:
"TestTemplate|| [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [END]"
它确实被迭代了 10 次,但是该变量it
似乎没有被填充。有任何想法吗?