Sitecore的“动态占位符”与MVC [英] Sitecore 'dynamic placeholders' with MVC
问题描述
我正在寻找在MVC工作动态占位符的解决方案。
有此模式为使用的WebForms至少有两个很好的描述:
I'm looking for a working Dynamic Placeholder solution in MVC. There are at least two good descriptions of this "pattern" for use with WebForms:
- http://trueclarity.word$p$pss.com/2012/06/19/dynamic-placeholder-keys-in-sitecore/
- http://www.techphoria414.com/Blog/2011/August/Dynamic_Placeholder_Keys_Prototype
和我也发现这个博客解释如何使用MVC做到这一点:
And I also found this blog explaining how to do it with MVC:
首先,我试图用技术从MVC博文(该SitecoreHelper的扩展)来实现Techphoria的方法(与GUID)的,我也尝试过实施的最后描述的方法(使用了递增COLUMN_1,COLUMN_2等的后缀)。
First I have tried to implement Techphoria's method (with GUIDs) using techniques from the MVC blogpost (extension of the SitecoreHelper) and I also tried implementing the last described method (uses number suffixes that are incremented Column_1, Column_2, etc).
对于所有我想我并没有创造一个可行的解决方案取得成功的变化。我的占位符没有得到正确命名(我结束了奇怪的占位符结构或占位符重复自己)。
With all the variations I tried I didn't succeed in creating a working solution. My placeholders don't get properly named (I ended up with strange placeholder structures, or placeholders repeating themselves).
没有进入我尝试的具体情况,我想知道其他人有没有准备好,我可以用一个有效的解决方案。
如果我不能找到一个已经工作的解决方案,我将描述我的问题,更详细,看看我能得到那个工作。
If I can't find an already working solution, I will describe my problem in more detail and see if I can get that to work.
推荐答案
我创造了这个扩展,创建动态placholders
I created this extension that creates dynamic placholders
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管道为占位符设置一个步骤,并检查
It creates a placeholder with the guid in the name. I also created a step in the pipeline that extracts the guid, and checks for placeholder settings.
code获得占位符设置的动态占位符
如果创建@ Html.Sitecore()动态占位DynamicPlaceholder(测试) - 以下code从名为test的占位符设置需要设置
Code to get placeholder settings to the dynamic placeholder If you create a dynamic placeholder with @Html.Sitecore().DynamicPlaceholder("test") - the following code takes the setting from the placeholder settings named 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);
}
}
}
以下code将删除该从Chrome中的数据pageeditor的GUID
The following code removes the guid from the chrome data in the pageeditor
/// <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包括设置如下包括:
The web.config include settings are included below:
<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>
这篇关于Sitecore的“动态占位符”与MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!