2013-02-28 9 views
22

Szukam działającego dynamicznego rozwiązania zastępczego w MVC. Istnieją co najmniej dwa dobre opisy tego "wzór" do użytku z WebForms:Dynamiczne elementy zastępcze Sitecore z MVC

I ja również znaleźć ten blog wyjaśniający jak to zrobić z MVC:

Najpierw starałem się zaimplementować metodę Techphorii (z identyfikatorami GUID) za pomocą technik z bloga MVC (rozszerzenie SitecoreHelper), a także próbowałem implementacji ostatnio opisanej metody (używa przyrostków liczb zwiększanych Kolumna_1, Kolumna_2, itp.).

Przy wszystkich odmianach, które wypróbowałem, nie udało mi się stworzyć działającego rozwiązania. Moje obiekty zastępcze nie zostały poprawnie nazwane (skończyłem z dziwnymi strukturami zastępczymi lub powtarzającymi się symbolami zastępczymi).

Bez wchodzenia w szczegóły moich prób, chciałbym wiedzieć, czy ktoś inny ma gotowe rozwiązanie robocze, które mógłbym wykorzystać.

Jeśli nie mogę znaleźć już działającego rozwiązania, opiszę mój problem bardziej szczegółowo i zobaczę, czy mogę go uruchomić.

Odpowiedz

40

stworzyłem tego rozszerzenia, która tworzy dynamiczne 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)); 
    } 
} 

Tworzy zastępczy z GUID w nazwie. Utworzyłem również krok w potoku, który wyodrębnia GUID i sprawdza ustawienia zastępcze.

kod, aby uzyskać ustawienia zastępcze z dynamicznym zastępczy Jeśli utworzyć dynamiczną zastępczy z @ Html.Sitecore() DynamicPlaceholder („test”) -. Poniższy kod wykonuje ustawienie z ustawień zastępczych nazwie 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); 
     } 
    } 
} 

Poniższy kod usuwa GUID z danych chromowane w 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; 
       } 
      } 
     } 
    } 
} 

Edit

web.config obejmują ustawienia są zawarte poniżej:

<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> 
+0

Mam zamiar przetestować tę pierwszą rzecz dziś wieczorem. Dzięki! –

+0

Działa, Dunston! Dziękuję Ci bardzo!!! –

+3

NP - Zaktualizowałem trochę kodu, aby zmienić wyświetlaną nazwę elementu zastępczego edytora, nie trzeba widzieć identyfikatora – dunston

0

Pobrałem pakiet Integrated Dynamic Placeholders z rynku Sitecore. Podczas budowania strony zwiększa symbole zastępcze z konfigurowalnym sufiksem dołączonym do końca klawisza zastępczego, aby uczynić je unikalnymi, zgodnie z ich kolejnością na warstwie prezentacji. Opracowałem dla mnie po wyjęciu z pudełka.

Powiązane problemy