Nov 7, 2016

Iterating over string array in template in SDL Tridion 2013 SP1

Now we can add any string array to template package and iterate over its members in Dreamweaver template. We need to serialize your array and then create package string item out of it as mentioned below:

var list = new[] { "aaa", "bbb", "ccc" };           

StringWriter stringWriter = new StringWriter();
XmlSerializer xmlSerializer = new XmlSerializer(list.GetType());
xmlSerializer.Serialize(stringWriter, list); 

package.PushItem("ItemName",
                  package.CreateStringItem(ContentType.StringArray, stringWriter.ToString()));

The DWT for this array will be as:
  <!-- TemplateBeginRepeat name="ItemName" -->
    <div>@@Value@@</div>
  <!-- TemplateEndRepeat -->

No comments: