临时更改Sitecore的项目的布局 [英] Temporarily change a Sitecore item's layout

查看:107
本文介绍了临时更改Sitecore的项目的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这个code我设法更改当前项目的效果图。然而,这permenantly改变了它在Sitecore的(该变化可以在CMS可以看出),而不是暂时,如我所料。

Using this code I managed to change the renderings on the current item. However this changed it permenantly in Sitecore (the changes were could be seen in the CMS) and not temporarily, as I expected.

void ReplaceLayout(Item item)
{
    if (item == null)
        return;

    using (new SecurityDisabler())
    {
        // New item
        LayoutField newLayoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.LayoutField]);
        LayoutDefinition newLayoutDefinition = LayoutDefinition.Parse(newLayoutField.Value);

        DeviceDefinition newDeviceDefinition = newLayoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());

        // Current item
        LayoutField layoutField = new LayoutField(Sitecore.Context.Item.Fields[Sitecore.FieldIDs.LayoutField]);
        LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);

        DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());
        deviceDefinition.Layout = newDeviceDefinition.Layout;
        deviceDefinition.Renderings = newDeviceDefinition.Renderings;

        Sitecore.Context.Item.Editing.BeginEdit();
        layoutField.Value = layoutDefinition.ToXml();
        Sitecore.Context.Item.Editing.EndEdit();
    }
}

我不想让permenant更改的项目,我只是想以取代在飞行中当前显示的项目效果图,如果某些条件得到满足。有谁知道如何改变一个项目的布局,这种方式?

I don't want to make permenant changes to the item, I just want to replace the currently displayed items renderings on the fly if some conditions are met. Does anyone know how to alter an item's layout in this way?

推荐答案

您在您的意见要在侧边栏显示某些sublayouts取决于某种形式的地区/步解释。 你可以通过添加,将适合的sublayouts一个占位符(例如,在你的侧边栏),并使用该code动态渲染sublayouts吧。

You explained in your comments that you want to display certain sublayouts in the sidebar depending on certain form parts/steps. You can do that by adding a PlaceHolder that will fit the sublayouts (e.g. in your sidebar) and use this code to dynamically render sublayouts to it.

首先,您需要在它的presentation设置配置的sublayout一个项目(我把它叫做一个片段项)。 然后你可以用code来呈现占位符(phSideBarPlaceHolder)内的项目。

First you need an item (i call it a snippet item) that has the sublayout configured on its presentation settings. Then you can use code to render that item inside the placeholder (phSideBarPlaceHolder).

// Load snippet item
Item snippet = Sitecore.Context.Database.GetItem("{id-or-path-of-snippet-item}");

// 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
phSideBarPlaceHolder.Controls.Add(sublayout);

如果您需要了解如何读取里面的数据sublayout code DataSource属性的详细信息,马克乌尔西诺写了关于这个问题的文章:的 http://firebreaksice.com/using-the-datasource-field-with-sitecore-sublayouts

If you need more info about how to read data the DataSource property inside the sublayout code, Mark Ursino has written an article about that: http://firebreaksice.com/using-the-datasource-field-with-sitecore-sublayouts

这篇关于临时更改Sitecore的项目的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆