Reputation: 2667
Lets say I have a parent item and three child items in sitecore cms
Home
-ChildOne
-ChildTwo
-ChildThree
I want to loop through all child items and display them using their individual sublayouts.
Is this possible and what do I need to do to achieve this.
At the moment, I can display one item in one placeholder, I am thinking of using a repeater to do this.
What are my best options? Is this actually possible? Are there any draw backs with the method you may suggest to me?
Upvotes: 1
Views: 2792
Reputation: 31435
You can accomplish this by using the <sc:sublayout ... />
control from Sitecore.
First, you need to make each sublayout for those access a DataSource item. Here is sample code I've blogged on the topic.
Next, you need to repeat over the children and bind them to the sublayout control while passing each item in as the DataSource:
Front-end:
<asp:Repeater ID="myRepeater" OnItemDataBound="myRepeater_ItemDataBound" runat="server">
<ItemTemplate>
<sc:sublayout ID="scSublayout" Path="path/to/your/sublayout/file.ascx" runat="server" />
</ItemTemplate>
</asp:Repeater>
Code-behind:
// in the Page_Load
myRepeater.DataSource = homeItem.GetChildren();
myRepeater.DataBind();
protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var scSublayout = e.Item.FindControl("scSublayout") as Sitecore.Web.UI.WebControls.Sublayout;
if (scSublayout != null)
{
scSublayout.DataSource = ((Sitecore.Data.Items.Item)e.Item.DataItem).ID.ToString();
}
}
}
Upvotes: 2
Reputation: 2953
From the wording of your question you seem to want to be able to display all child items whatever their type. Your difficulty here will be determining which sublayout to use to display each of the children.
Sitecore doesn't bind an item to a single sublayout though. Firstly it can have a whole series of sublayouts bound to it, secondly which sublayouts are bound to it is specified at item level (you can set default presentation details on the template standard values, but this can be overridden by the item itself), thirdly, the item may have different sublayouts bound to it according to which device context it is being viewed in. Unless you find a way to configure an item to have a single sublayout for use when it's being looped through by a parent item you're likely to end up with something that's either very messy (lots of sublayouts being unpacked, rendered, nested - tagetting the same placeholders) or something that is exceptionally unflexible (an item can only have one sublayout, and this sublayout is limited in how it can behave).
A better approach would be to determine exactly what it is about each item type that you'd like to display when it's being viewed alongside its siblings. If you're looking for similar information from each item then take this information and maybe use it as a base for a template that all your items derive from at some level (so they all have common fields). Then write a sublayout that does the looping and can use something like a repeater to get the same fields for each item.
If your items are radically different then your looping sublayout will have to be able to match different cases and contain or use relevant renderings for each case.
Hope that helps.
Upvotes: 1