这是我正在尝试做的事情:
我有一个内容项目“轮播演示者”。本质上,这将在轮播中显示其子项。我希望灵活地拥有任意数量的子项目。我还希望能够灵活地指定每个子项目的呈现方式——它们可能相同或不同。我正在使用 Sitecore 6.5。
轮播是 jcarousel。我需要生成通常像这样的标记(来自项目“Carousel Presenter”):
<div class="jcarousel">
<ul>
<li> ... MARKUP FROM ITEM 1 ... </li>
<li> ... MARKUP FROM ITEM 2 ... </li>
... and so on
</ul>
</div>
这是我尝试过的:
创建子布局“carousel presenter.ascx”,标记:
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
// Get all children and render them inside the <ul>
var kids = Sitecore.Context.Item.GetChildren();
foreach (Item snippet in kids)
{
// RENDER THE ITEMS HERE INTO THE PLACEHOLDER...
// Get the first rendering from item's presentation definition
RenderingReference rendering = snippet.Visualization.GetRenderings(Sitecore.Context.Device, false).FirstOrDefault();
// We assume that its a Sublayout, but you can also check for xslt and create an XslFile() object
Sublayout sublayout = new Sublayout();
sublayout.DataSource = snippet.Paths.FullPath; // creates a reference to the snippet item, so you can pull data from that later on
sublayout.Path = rendering.RenderingItem.InnerItem["Path"];
sublayout.Cacheable = rendering.RenderingItem.Caching.Cacheable;
// Copy cache settings
if (rendering.RenderingItem.Caching.Cacheable)
{
sublayout.VaryByData = rendering.RenderingItem.Caching.VaryByData;
sublayout.VaryByDevice = rendering.RenderingItem.Caching.VaryByDevice;
sublayout.VaryByLogin = rendering.RenderingItem.Caching.VaryByLogin;
sublayout.VaryByParm = rendering.RenderingItem.Caching.VaryByParm;
sublayout.VaryByQueryString = rendering.RenderingItem.Caching.VaryByQueryString;
sublayout.VaryByUser = rendering.RenderingItem.Caching.VaryByUser;
}
// Now render the sublayout to the placeholder
carouselItemsPh.Controls.Add(sublayout);
}
}
请注意,我从这里窃取了大部分代码:临时更改 Sitecore 项目的布局
- 在内容树中创建子项,在内容项“轮播演示者”下方,即“轮播项”,并为每个子布局控件分配一个子布局控件(通过配置/布局中的标准值)。
一切都发布了。
当我点击测试页面时,会为每个子项(“轮播项”)生成标记,并且轮播可以工作,但看起来数据源没有正确分配 - 所有子项的数据源/上下文都是父项项,尽管在我创建子控件时明确设置了数据源。我该如何解决?
有没有更好的方法来实现我在 Sitecore 6.5 中实现的目标?
谢谢