本文介绍了Sitecore 8:通过默认渲染自动填充占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩动态占位符,并被一个预填充概念所震惊.有没有办法为我的占位符选择默认渲染,以避免在体验编辑器中出现选择渲染"对话框??

I was playing around with dynamic placeholders and was struck by a prefilling concept.Is there a way to select a default rendering for one of my placeholders which would avoid the "select rendering" dialog in experience editor ??

场景:我有一个名为PageHead"的渲染,其中包含三个渲染.其中之一是占位符PageTeaserPh",它目前允许两种渲染:一种是PageTeaser",第二种是PageTeaserWithImage".我希望占位符PageTeaserPh"始终将渲染选择为PageTeaser",从而避免出现选择渲染"对话框.

Scenario: I have a rendeing called "PageHead" which has three renderings. One of them is a placeholder "PageTeaserPh" which currently allows two renderings: one is "PageTeaser" and second "PageTeaserWithImage". I want the placeholder "PageTeaserPh" to always have the rendering selected as "PageTeaser" and therefore avoid the dialog "Select rendering" .

我做了一些功课,想知道这是否与标准值有关(我们可以在模板级别使用它;但不确定效果图)而且我也听说过命令模板概念(不深入).

I did some homework and was wondering if this is something related to Standard values (we can have it at template level; not sure for renderings though) and also i have heard of command template concept (not in-depth).

感谢所有帮助.

推荐答案

您可以在模板的标准值上分配渲染,然后每个新项目都会有您的 PageTeaser 渲染.

You can have renderings assigned on standard values of templates, each new item would then have your PageTeaser rendering.

如果您想自动执行此过程,请查看 <mvc.getXmlBasedLayoutDefinition> 管道,我们通过扩展此管道来注入通用渲染.

If you wanted to automate this process have a look at the <mvc.getXmlBasedLayoutDefinition> pipeline, we are injected common renderings by extending this pipeline.

更新

我发现了一些代码示例和博客文章,它们应该可以帮助您指明操作布局细节的正确方向.

I've found some code samples and blog posts that should help point you in the right direction for manipulating the layout details.

public void AddSublayoutToItem(string itemId, string sublayoutId)
{
    using (new Sitecore.SecurityModel.SecurityDisabler())
    {
        if (Sitecore.Data.ID.IsID(itemId) && Sitecore.Data.ID.IsID(sublayoutId))
        {
            //Get the master database and get the item on which you want to add sublayout
            Database masterDatabase = Database.GetDatabase("master");
            Item item = masterDatabase.GetItem(Sitecore.Data.ID.Parse(itemId));

            //  Or you can also get Sitecore Item from Context Database as per your requirement
            //  Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));

            if (item != null)
            {
                // Get the layout definitions and the device definition
                LayoutField layoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.LayoutField]);
                LayoutDefinition layoutDefinition = LayoutDefinition.Parse(layoutField.Value);
                DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(Sitecore.Context.Device.ID.ToString());

                //Create a RenderingDefinition and add the reference of sublayout or rendering
                RenderingDefinition renderingDefinition = new RenderingDefinition();
                renderingDefinition.ItemID = sublayoutId;
                //Set placeholder where the rendering should be displayed
                renderingDefinition.Placeholder = "content";
                // Set the datasource of sublayout, if any
                renderingDefinition.Datasource = "{24240FF2-B4AA-4EB2-B0A4-63E027934C38}";

                // you can also set datasource of sublayout using Sitecore Path
                // renderingDefinition.Datasource = "/sitecore/content/Home/Books";

                //Add the RenderingReference to the DeviceDefinition
                deviceDefinition.AddRendering(renderingDefinition);

                // Save the layout changes
                item.Editing.BeginEdit();
                layoutField.Value = layoutDefinition.ToXml(); ;
                item.Editing.EndEdit();
            }
        }
    }
}

取自这里 - http://www.bugdebugzone.com/2014/06/how-to-add-sublayout-to-sitecore-item.html

还有一些关于该主题的其他博客

Also a couple of other blogs on the topic

这篇关于Sitecore 8:通过默认渲染自动填充占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-28 20:48
查看更多