呈现旋转木马控制自己的布局子项 [英] Render child items with their own layout in a carousel control
问题描述
下面就是我要做的:
我有一个内容项旋转木马presenter。本质上,这将present转盘内的子项。我想要的灵活性,有任意数量的子项。我还希望可以灵活地能够指定每个子项的presentation - 它们可以相同或不同。我使用Sitecore的6.5。
I have a content item "Carousel Presenter". Essentially this will present its child items within the carousel. I want the flexibility to have any number of child items. I also want the flexibility to be able to specify the presentation of each child item - they may be the same or different. I am using Sitecore 6.5.
轮播是的jCarousel。我需要生成的标记一般是这样(从项目旋转木马presenter):
The carousel is jcarousel. I need to generate markup generally like this (from item "Carousel Presenter"):
<div class="jcarousel">
<ul>
<li> ... MARKUP FROM ITEM 1 ... </li>
<li> ... MARKUP FROM ITEM 2 ... </li>
... and so on
</ul>
</div>
下面是我曾尝试:
-
创建sublayout旋转木马presenter.ascx标记:
Created sublayout "carousel presenter.ascx", markup:
codebehind:
Codebehind:
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);
}
}
请注意,我偷了最该code从这里:<一href=\"http://stackoverflow.com/questions/9666805/temporarily-change-a-sitecore-items-layout\">Temporarily更改Sitecore的项目的布局
Note that I stole most of this code from here: Temporarily change a Sitecore item's layout
- 在内容树中创建子项,内容项下的旋转木马presenter,这是旋转木马项目,用(在配置/布局通过标准值)分配给每个sublayout控制。
一切都公开。
当我打的每个子项(旋转木马项目),并转盘作品产生我的测试页面的标记,但它看起来像数据源没有被正确分配 - 所有子项的数据源/背景是父项,尽管明确设定DataSource的,当我创建的子控件。我该如何解决这个问题?
When I hit my test page markup is generated for each of the child items ("Carousel Items"), and the carousel works, but it looks like the Datasource is not being assigned properly - the datasource/context of all child items is the parent item, despite setting the Datasource explicitly when I create the child controls. How do I fix this?
有没有什么,我想在Sitecore的6.5实现了更好的方法?
Is there a better approach to what I am trying to achieve in Sitecore 6.5?
感谢
推荐答案
在用户控件/为您的孩子的物品sublayouts需要以编程方式读取数据源。对于这份工作,我总是有我自己的'基地'Sublayout用于处理数据源问题,用户控件类。在我的基类我默认如果数据源尚未设置使用Sitecore.Context.Item。在code是如下:
The user controls/sublayouts for your child items need to programmatically read the datasource. For this job I always have my own 'base' Sublayout class which handles the datasource issue for user controls. In my base class I default to using Sitecore.Context.Item if the datasource has not been set. The code is as follows:
public class SublayoutBase : UserControl
{
private Item _dataSource;
public Item DataSource
{
get
{
if (_dataSource == null)
{
if (Parent is Sublayout)
{
_dataSource =
Sitecore.Context.Database.GetItem(((Sublayout)Parent).DataSource);
}
if (_dataSource == null)
{
_dataSource = Sitecore.Context.Item;
}
}
return _dataSource;
}
}
protected override void OnLoad(EventArgs e)
{
foreach (Control c in Controls)
{
SetFieldRenderers(DataSource, c);
}
base.OnLoad(e);
}
private void SetFieldRenderers(Item item, Control control)
{
if (item != null)
{
var ctrl = control as Sitecore.Web.UI.WebControl;
if (ctrl != null && !string.IsNullOrEmpty(ctrl.DataSource))
{
//don't set the source item if the DataSource has already been set.
return;
}
if (control is FieldRenderer)
{
var fr = (FieldRenderer)control;
fr.Item = item;
}
else if (control is Image)
{
var img = (Image)control;
img.Item = item;
}
else if (control is Link)
{
var link = (Link)control;
link.Item = item;
}
else if (control is Text)
{
var text = (Text)control;
text.Item = item;
}
else
{
foreach (Control childControl in control.Controls)
{
SetFieldRenderers(item, childControl);
}
}
}
}
}
这篇关于呈现旋转木马控制自己的布局子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!