Asp.net
使用 MVC 的 Sitecore“動態佔位符”
我正在 MVC 中尋找一個有效的動態佔位符解決方案。對於與 WebForms 一起使用的這種“模式”,至少有兩個很好的描述:
- <http://trueclarity.wordpress.com/2012/06/19/dynamic-placeholder-keys-in-sitecore/>
- <http://www.techphoria414.com/Blog/2011/August/Dynamic_Placeholder_Keys_Prototype>
而且我還發現這個部落格解釋瞭如何使用 MVC 做到這一點:
- <http://blogs.perficient.com/portals/2012/10/17/sitecore-mvc-dynamic-placeholders/>
首先,我嘗試使用 MVC 部落格文章(SitecoreHelper 的擴展)中的技術來實現 Techphoria 的方法(使用 GUID),並且我還嘗試實現最後描述的方法(使用遞增 Column_1、Column_2 等的數字後綴)。
通過我嘗試的所有變體,我沒有成功創建一個有效的解決方案。我的佔位符沒有正確命名(我最終得到了奇怪的佔位符結構,或者佔位符重複自己)。
在不詳細說明我的嘗試的細節的情況下,我想知道是否有其他人準備好我可以使用的可行解決方案。
如果我找不到一個已經可行的解決方案,我會更詳細地描述我的問題,看看我是否可以讓它發揮作用。
我創建了這個創建動態佔位符的擴展
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); } } }以下程式碼從 pageeditor 中的 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>