代码之家  ›  专栏  ›  技术社区  ›  Ruud van Falier

使用MVC的Sitecore“动态占位符”

  •  24
  • Ruud van Falier  · 技术社区  · 12 年前

    我正在寻找一个在MVC中工作的动态占位符解决方案。 对于这种“模式”,至少有两种很好的描述可用于WebForms:

    我还发现这个博客解释了如何使用MVC实现这一点:

    首先,我尝试使用MVC博客文章中的技术(SitecoreHelper的扩展)来实现Techphoria的方法(使用GUID),我还尝试实现最后描述的方法(采用递增的数字后缀Column_1、Column_2等)。

    尽管我尝试了各种变体,但我没有成功地创建出一个有效的解决方案。我的占位符没有得到正确的命名(我最终得到了奇怪的占位符结构,或者占位符重复)。

    在不详细介绍我尝试的情况下,我想知道是否有其他人准备好了我可以使用的工作解决方案。

    如果我找不到一个已经有效的解决方案,我会更详细地描述我的问题,看看我是否能让它发挥作用。

    2 回复  |  直到 10 年前
        1
  •  42
  •   Matthew Dresser    10 年前

    我创建了这个扩展,它可以创建动态占位符

    public static class SitecoreHelper
    {
        public static HtmlString DynamicPlaceholder(this Sitecore.Mvc.Helpers.SitecoreHelper helper, string dynamicKey)
        {
            var currentRenderingId = RenderingContext.Current.Rendering.UniqueId;
            return helper.Placeholder(string.Format("{0}_{1}", dynamicKey, currentRenderingId));
        }
    }
    

    它在名称中创建一个带有guid的占位符。 我还在管道中创建了一个步骤,用于提取guid,并检查占位符设置。

    用于将占位符设置获取到动态占位符的代码 如果使用@Html.Sitecore().DynamicPlaceholder(“test”)创建动态占位符,则以下代码将从名为test的占位符设置中获取设置

     /// <summary>
    /// Handles changing context to the references dynamic "master" renderings settings for inserting the allowed controls for the placeholder and making it editable
    /// </summary>
    public class GetDynamicKeyAllowedRenderings : GetAllowedRenderings
    {
        //text that ends in a GUID
        private const string DYNAMIC_KEY_REGEX = @"(.+)_[\d\w]{8}\-([\d\w]{4}\-){3}[\d\w]{12}";
    
        public new void Process(GetPlaceholderRenderingsArgs args)
        {
            Assert.IsNotNull(args, "args");
    
            string placeholderKey = args.PlaceholderKey;
            Regex regex = new Regex(DYNAMIC_KEY_REGEX);
            Match match = regex.Match(placeholderKey);
            if (match.Success && match.Groups.Count > 0)
            {
                placeholderKey = match.Groups[1].Value;
            }
            else
            {
                return;
            }
            // Same as Sitecore.Pipelines.GetPlaceholderRenderings.GetAllowedRenderings but with fake placeholderKey
            Item placeholderItem = null;
            if (ID.IsNullOrEmpty(args.DeviceId))
            {
                placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
                                                                 args.LayoutDefinition);
            }
            else
            {
                using (new DeviceSwitcher(args.DeviceId, args.ContentDatabase))
                {
                    placeholderItem = Client.Page.GetPlaceholderItem(placeholderKey, args.ContentDatabase,
                                                                     args.LayoutDefinition);
                }
            }
            List<Item> collection = null;
            if (placeholderItem != null)
            {
                bool flag;
                args.HasPlaceholderSettings = true;
                collection = this.GetRenderings(placeholderItem, out flag);
                if (flag)
                {
                    args.CustomData["allowedControlsSpecified"] = true;
                    args.Options.ShowTree = false;
                }
            }
            if (collection != null)
            {
                if (args.PlaceholderRenderings == null)
                {
                    args.PlaceholderRenderings = new List<Item>();
                }
                args.PlaceholderRenderings.AddRange(collection);
            }
        }
    }
    

    以下代码从页面编辑器中的chrome数据中删除guid

    /// <summary>
    /// Replaces the Displayname of the Placeholder rendering with the dynamic "parent"
    /// </summary>
    public class GetDynamicPlaceholderChromeData : GetChromeDataProcessor
    {
        //text that ends in a GUID
        private const string DYNAMIC_KEY_REGEX = @"(.+)_[\d\w]{8}\-([\d\w]{4}\-){3}[\d\w]{12}";
    
        public override void Process(GetChromeDataArgs args)
        {
            Assert.ArgumentNotNull(args, "args");
            Assert.IsNotNull(args.ChromeData, "Chrome Data");
            if ("placeholder".Equals(args.ChromeType, StringComparison.OrdinalIgnoreCase))
            {
                string argument = args.CustomData["placeHolderKey"] as string;
    
                string placeholderKey = argument;
                Regex regex = new Regex(DYNAMIC_KEY_REGEX);
                Match match = regex.Match(placeholderKey);
                if (match.Success && match.Groups.Count > 0)
                {
                    // Is a Dynamic Placeholder
                    placeholderKey = match.Groups[1].Value;
                }
                else
                {
                    return;
                }
    
                // Handles replacing the displayname of the placeholder area to the master reference
                Item item = null;
                if (args.Item != null)
                {
                    string layout = ChromeContext.GetLayout(args.Item);
                    item = Sitecore.Client.Page.GetPlaceholderItem(placeholderKey, args.Item.Database, layout);
                    if (item != null)
                    {
                        args.ChromeData.DisplayName = item.DisplayName;
                    }
                    if ((item != null) && !string.IsNullOrEmpty(item.Appearance.ShortDescription))
                    {
                        args.ChromeData.ExpandedDisplayName = item.Appearance.ShortDescription;
                    }
                }
            }
        }
    }
    

    编辑

    web.config包含的设置如下所示:

    <sitecore>
      <pipelines>
    
        <getPlaceholderRenderings>
          <processor 
            type="YourNamespace.Pipelines.GetPlaceholderRenderings.GetDynamicKeyAllowedRenderings, YourAssembly"
            patch:before="processor[@type='Sitecore.Pipelines.GetPlaceholderRenderings.GetAllowedRenderings, Sitecore.Kernel']"/>
        </getPlaceholderRenderings>
    
        <getChromeData>
          <processor
            type="YourNamespace.Pipelines.GetChromeData.GetDynamicPlaceholderChromeData, YourAssembly"
            patch:after="processor[@type='Sitecore.Pipelines.GetChromeData.GetPlaceholderChromeData, Sitecore.Kernel']"/>
        </getChromeData>
    
      </pipelines>
    </sitecore> 
    
        2
  •  0
  •   Jabare Mitchell    9 年前

    我下载了 Integrated Dynamic Placeholders 来自sitecore市场的软件包。在构建页面时,它会根据占位符在表示层上的顺序,在占位符键的末尾添加可配置后缀,以使其唯一。为我打破常规。