In the StringTemplate4 cheat sheet (http://www.antlr.org/wiki/display/ST/StringTemplate+cheat+sheet), it mentions that to perform an iteration
<attribute:{anonymous-template}>
Apply an anonymous template to each element of attribute. The iterated `it` attribute is set automatically.
I have tried the below code:
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);
Update 1
Below are the TestModel data structure and related classes. I am using these just
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();
}
}
Yet the end result is:
"TestTemplate|| [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [DataList ] [END]"
It does get iterated 10 times, however the variable it
does not seem to have been populated. Any ideas?
You need to change your template to this
TestTemplate|| <List:{x | [DataList <x.Name>] }> [END]
As of this documentation
<attribute:{x | anonymous-template}>
Apply an anonymous template to each element of attribute.
The iterated value is set to argument x.
The anonymous template references <x> to access the iterator value.
You can set x
to anything you like, it's just a place holder for the iterator.
The problem was that you were using the StringTemplate3 cheatsheet instead of the StringTemplate4 cheatsheet http://www.antlr.org/wiki/display/ST4/StringTemplate+cheat+sheet