Looping through all Component Presentations
If the Component Presentations on a Page are not assembled at visit time using a query, the Page contains a list of Component Presentations (pairs of a Component and its accompanying Component Template). Typically, you would want to loop through these Component Presentations to render each one.
The package that is processed by a Compound Page Template initially only contains a Page item. To get to the Component Presentations on the Page, use the default Template Building Block called "Extract Components from Page". This adds a Components array to the package, containing the Component Presentations. The Component Presentations in this array are typically rendered in the Adobe Dreamweaver Template Building Block.
However, in some cases, you may want to examine or modify the Component Presentations before offering them to the Dreamweaver Template. For example, you might want to put some of the Component Presentations on the main content area of your Web page, and others in a sidebar area.
Package example
The following code shows you how to access Component Presentations on a Page in the package from your C# code. In this example, the array of Component Presentations is split into two separate lists, which are given the names ArticleComponents and QuickNewsComponents, based on the Schema of each Component. Note that by doing this, the next Template Building Block in the Page Template must expect these two lists, rather than the Components array.
// Get the Components array.
// Package.ComponentsName is a constant for the string "Components".
Item componentsItem = package.GetByName(Package.ComponentsName);
IComponentPresentationList componentPresentations =
ComponentPresentationList.FromXml(componentsItem.GetAsString());
// Create two lists for the two types of Component Presentations
ComponentPresentationList articleCPs = new ComponentPresentationList();
ComponentPresentationList quickNewsCPs = new ComponentPresentationList();
// Loop through the CPs and divide them over the two lists.
foreach (Tridion.ContentManager.Templating.ComponentPresentation
componentPresentation in componentPresentations)
{
Component component =
(Component) engine.GetSession().GetObject(componentPresentation.ComponentUri);
if (component.Schema.Title == "Article") {
articleCPs.Add(componentPresentation);
}
else if (component.Schema.Title == "QuickNews") {
quickNewsCPs.Add(componentPresentation);
}
else {
log.Warning("Component with unknown Schema " +
component.Schema.Title + " ignored.");
}
}
// Add each list to the package.
Item articlesItem =
package.CreateStringItem(ContentType.ComponentArray,
ComponentPresentationList.ToXml(articleCPs));
package.PushItem("ArticleComponents", articlesItem);
Item quickNewsItem =
package.CreateStringItem(ContentType.ComponentArray,
ComponentPresentationList.ToXml(articleCPs));
package.PushItem("QuickNewsComponents", quickNewsItem);
// Remove the original Components array
package.Remove(componentsItem);