private RenderingChrome GetPlaceHolderChrome(string placeholderKey) { var result = new RenderingChrome(); var wrapper = new global::Sitecore.Mvc.ExperienceEditor.Pipelines.Response.RenderPlaceholder.AddWrapper(); using (var sw = new StringWriter()) { var sb = sw.GetStringBuilder(); var args = new RenderPlaceholderArgs(placeholderKey, sw) { }; using (PlaceholderContext.Enter(new PlaceholderContext(args.PlaceholderName))) { wrapper.Process(args); result.Start = sb.ToString(); sb.Clear(); foreach (var disposable in args.Disposables) { disposable.Dispose(); } } result.End = sb.ToString(); } return(result); }
public override void Process(RenderPlaceholderArgs args) { Assert.ArgumentNotNull((object)args, "args"); var marker = this.GetMarker(); if (marker == null) return; args.Disposables.Add(new Wrapper(args.Writer, marker)); }
protected virtual IEnumerable<Rendering> GetRenderings(string placeholderName, RenderPlaceholderArgs args) { Guid deviceId = this.GetPageDeviceId(args); var childRenderings = new List<Rendering>(); if (placeholderName.Contains("|")) { string[] placeholderParts = placeholderName.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); // 0 = Placeholder Name, 1 = UniqueId (For the Current Rendering) List<Rendering> lstRenderings = args.PageContext.PageDefinition.Renderings; if (placeholderParts.Count() > 1) { string renderingId = string.Empty; bool foundCurrentRendering = false; foreach (Rendering rendering in lstRenderings) { if (rendering.DeviceId != deviceId) continue; if (rendering.UniqueId.ToString().Equals(placeholderParts[1]) || foundCurrentRendering) { // Store RenderingItemPath which is common across all renderings of this type if (!foundCurrentRendering) renderingId = rendering.RenderingItemPath; // Exit Foreach When Next Rendering of Same Type as the current one is found if (foundCurrentRendering && rendering.RenderingItemPath.Equals(renderingId)) break; // Only Add Renderings that match Placeholdername if (rendering.Placeholder.Equals(placeholderParts[0])) { childRenderings.Add(rendering); } foundCurrentRendering = true; } } } } else { string placeholderPath = StringExtensions.OrEmpty(ObjectExtensions.ValueOrDefault<PlaceholderContext, string>(PlaceholderContext.Current, (Func<PlaceholderContext, string>)(context => context.PlaceholderPath))); childRenderings = Enumerable.Where<Rendering>((IEnumerable<Rendering>)args.PageContext.PageDefinition.Renderings, (Func<Rendering, bool>)(r => { if (!(r.DeviceId == deviceId)) return false; if (!StringExtensions.EqualsText(r.Placeholder, placeholderName)) return StringExtensions.EqualsText(r.Placeholder, placeholderPath); return true; })).ToList(); } return childRenderings; }
protected virtual Guid GetPageDeviceId(RenderPlaceholderArgs args) { Guid guid1 = ObjectExtensions.ValueOrDefault<Rendering, Guid>(args.OwnerRendering, (Func<Rendering, Guid>)(rendering => rendering.DeviceId)); if (guid1 != Guid.Empty) return guid1; Guid guid2 = ObjectExtensions.ValueOrDefault<Rendering, Guid>(ObjectExtensions.ValueOrDefault<RenderingView, Rendering>(PageContext.Current.PageView as RenderingView, (Func<RenderingView, Rendering>)(view => view.Rendering)), (Func<Rendering, Guid>)(rendering => rendering.DeviceId)); if (guid2 != Guid.Empty) return guid2; return Context.Device.ID.ToGuid(); }
public override void Process(RenderPlaceholderArgs args) { Assert.ArgumentNotNull((object)args, "args"); var marker = this.GetMarker(); if (marker == null) { return; } args.Disposables.Add(new Wrapper(args.Writer, marker)); }
protected virtual void Render(string placeholderName, TextWriter writer, RenderPlaceholderArgs args) { IEnumerable <Rendering> renderings = this.GetRenderings(placeholderName, args); if (renderings != null) { foreach (Rendering rendering in renderings) { PipelineService.Get().RunPipeline <RenderRenderingArgs>("mvc.renderRendering", new RenderRenderingArgs(rendering, writer)); } } }
/// <summary> /// Override the standard get renderings for placeholder. Looks for a fallback device and uses the renderings /// from that device instead of the current device /// </summary> /// <param name="placeholderName"> /// The placeholder name. /// </param> /// <param name="args"> /// The <see cref="RenderPlaceholderArgs"/> for the pipeline /// </param> /// <returns> /// An <see cref="IEnumerable"/> of <see cref="Rendering"/> objects for the current placeholder/device /// </returns> protected override IEnumerable<Rendering> GetRenderings(string placeholderName, RenderPlaceholderArgs args) { var placeholderPath = PlaceholderContext.Current.ValueOrDefault(context => context.PlaceholderPath).OrEmpty(); var deviceId = this.GetPageDeviceId(args); // Get the current device var device = Context.Resources.Devices[new ID(deviceId)]; var usingFallbackDevice = false; var ajaxDevice = device.InnerItem["ajax device"]; if (string.IsNullOrWhiteSpace(ajaxDevice) == false) { Tracer.Info($"Current device is an AJAX device, using renderings from the fallback device [{ajaxDevice}]"); // Get the renderings from the fallback device deviceId = Guid.Parse(ajaxDevice); usingFallbackDevice = true; } var renderings = new List<Rendering>(); renderings.AddRange( args.PageContext.PageDefinition.Renderings.Where( r => { if (!(r.DeviceId == deviceId)) { return false; } return r.Placeholder.EqualsText(placeholderName) || r.Placeholder.EqualsText(placeholderPath); })); var renderingItems = renderings.ToList(); if (usingFallbackDevice) { // Check each rendering for an ajax version foreach (var rendering in renderings) { var ajaxRendering = this.GetAjaxVersion(rendering); if (ajaxRendering != null) { // There is an ajax version, so insert it into the same place as // the original rendering and remove the original. var indexOfRendering = renderingItems.IndexOf(rendering); renderingItems.Insert(indexOfRendering, ajaxRendering); renderingItems.Remove(rendering); } } } return renderingItems; }
protected virtual Guid GetPageDeviceId(RenderPlaceholderArgs args) { Guid guid1 = ObjectExtensions.ValueOrDefault <Rendering, Guid>(args.OwnerRendering, (Func <Rendering, Guid>)(rendering => rendering.DeviceId)); if (guid1 != Guid.Empty) { return(guid1); } Guid guid2 = ObjectExtensions.ValueOrDefault <Rendering, Guid>(ObjectExtensions.ValueOrDefault <RenderingView, Rendering>(PageContext.Current.PageView as RenderingView, (Func <RenderingView, Rendering>)(view => view.Rendering)), (Func <Rendering, Guid>)(rendering => rendering.DeviceId)); if (guid2 != Guid.Empty) { return(guid2); } return(Context.Device.ID.ToGuid()); }
protected override void Render(string placeholderName, TextWriter writer, RenderPlaceholderArgs args) { var renderingsForPlaceholder = GetRenderings(placeholderName, args).ToList(); if (!renderingsForPlaceholder.Any()) { var unusedPlaceholders = Context.Items["unusedPlaceholderKeys"] as List <string> ?? new List <string>(); unusedPlaceholders.Add(placeholderName); Context.Items["unusedPlaceholderKeys"] = unusedPlaceholders; } foreach (Rendering rendering in renderingsForPlaceholder) { if (rendering != null) { using (CreateCyclePreventer(placeholderName, rendering)) { ProcessRenderRendering(rendering, writer); } } } }
/// <summary> /// Render step, except it temporarily abandons the placeholder context to render a seperate item, after which it puts the context back /// </summary> /// <param name="placeholderName">Placeholder to render</param> /// <param name="writer">writer to render to</param> /// <param name="args"></param> protected override void Render(string placeholderName, TextWriter writer, RenderPlaceholderArgs args) { if (PageRenderItemDefinitionContext.CurrentOrNull != null) { args.PageContext.PageDefinition = PageRenderItemDefinitionContext.Current.Definition; } if (placeholderName != ItemRenderingKey) { base.Render(placeholderName, writer, args); return; } Stack <PlaceholderContext> previousContext = new Stack <PlaceholderContext>(); while (PlaceholderContext.CurrentOrNull != null) { previousContext.Push(PlaceholderContext.Current); PlaceholderContext.Exit(); } try { PipelineService.Get().RunPipeline("mvc.renderRendering", new RenderRenderingArgs(args.PageContext.PageDefinition.Renderings.First(x => x.Placeholder.IsWhiteSpaceOrNull()), writer)); } finally { while (PlaceholderContext.CurrentOrNull != null) { PlaceholderContext.Exit(); } while (previousContext.Any()) { PlaceholderContext.Enter(previousContext.Pop()); } } }
protected virtual void Render(string placeholderName, TextWriter writer, RenderPlaceholderArgs args) { IEnumerable<Rendering> renderings = this.GetRenderings(placeholderName, args); if (renderings != null) foreach (Rendering rendering in renderings) PipelineService.Get().RunPipeline<RenderRenderingArgs>("mvc.renderRendering", new RenderRenderingArgs(rendering, writer)); }
public override void Process(RenderPlaceholderArgs args) { Assert.ArgumentNotNull((object)args, "args"); this.Render(args.PlaceholderName, args.Writer, args); }
protected virtual IEnumerable <Rendering> GetRenderings(string placeholderName, RenderPlaceholderArgs args) { Guid deviceId = this.GetPageDeviceId(args); var childRenderings = new List <Rendering>(); if (placeholderName.Contains("|")) { string[] placeholderParts = placeholderName.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries); // 0 = Placeholder Name, 1 = UniqueId (For the Current Rendering) List <Rendering> lstRenderings = args.PageContext.PageDefinition.Renderings; if (placeholderParts.Count() > 1) { string renderingId = string.Empty; bool foundCurrentRendering = false; foreach (Rendering rendering in lstRenderings) { if (rendering.DeviceId != deviceId) { continue; } if (rendering.UniqueId.ToString().Equals(placeholderParts[1]) || foundCurrentRendering) { // Store RenderingItemPath which is common across all renderings of this type if (!foundCurrentRendering) { renderingId = rendering.RenderingItemPath; } // Exit Foreach When Next Rendering of Same Type as the current one is found if (foundCurrentRendering && rendering.RenderingItemPath.Equals(renderingId)) { break; } // Only Add Renderings that match Placeholdername if (rendering.Placeholder.Equals(placeholderParts[0])) { childRenderings.Add(rendering); } foundCurrentRendering = true; } } } } else { string placeholderPath = Sitecore.Mvc.Extensions.StringExtensions.OrEmpty(ObjectExtensions.ValueOrDefault <PlaceholderContext, string>(PlaceholderContext.Current, (Func <PlaceholderContext, string>)(context => context.PlaceholderPath))); childRenderings = Enumerable.Where <Rendering>((IEnumerable <Rendering>)args.PageContext.PageDefinition.Renderings, (Func <Rendering, bool>)(r => { if (!(r.DeviceId == deviceId)) { return(false); } if (!Sitecore.Mvc.Extensions.StringExtensions.EqualsText(r.Placeholder, placeholderName)) { return(Sitecore.Mvc.Extensions.StringExtensions.EqualsText(r.Placeholder, placeholderPath)); } return(true); })).ToList(); } return(childRenderings); }
/// <summary> /// Override the standard get renderings for placeholder. Looks for a fallback device and uses the renderings /// from that device instead of the current device /// </summary> /// <param name="placeholderName"> /// The placeholder name. /// </param> /// <param name="args"> /// The <see cref="RenderPlaceholderArgs"/> for the pipeline /// </param> /// <returns> /// An <see cref="IEnumerable"/> of <see cref="Rendering"/> objects for the current placeholder/device /// </returns> protected override IEnumerable <Rendering> GetRenderings(string placeholderName, RenderPlaceholderArgs args) { var placeholderPath = PlaceholderContext.Current.ValueOrDefault(context => context.PlaceholderPath).OrEmpty(); var deviceId = this.GetPageDeviceId(args); // Get the current device var device = Context.Resources.Devices[new ID(deviceId)]; var usingFallbackDevice = false; var ajaxDevice = device.InnerItem["ajax device"]; if (string.IsNullOrWhiteSpace(ajaxDevice) == false) { Tracer.Info($"Current device is an AJAX device, using renderings from the fallback device [{ajaxDevice}]"); // Get the renderings from the fallback device deviceId = Guid.Parse(ajaxDevice); usingFallbackDevice = true; } var renderings = new List <Rendering>(); renderings.AddRange( args.PageContext.PageDefinition.Renderings.Where( r => { if (!(r.DeviceId == deviceId)) { return(false); } return(r.Placeholder.EqualsText(placeholderName) || r.Placeholder.EqualsText(placeholderPath)); })); var renderingItems = renderings.ToList(); if (usingFallbackDevice) { // Check each rendering for an ajax version foreach (var rendering in renderings) { var ajaxRendering = this.GetAjaxVersion(rendering); if (ajaxRendering != null) { // There is an ajax version, so insert it into the same place as // the original rendering and remove the original. var indexOfRendering = renderingItems.IndexOf(rendering); renderingItems.Insert(indexOfRendering, ajaxRendering); renderingItems.Remove(rendering); } } } return(renderingItems); }
/// <summary> /// Renders an item with a layout defined to a string for MVC /// </summary> /// <returns>HTML of item</returns> public virtual void Render(TextWriter writer) { var originalDisplayMode = Context.Site.DisplayMode; // keep a copy of the renderings we start with. // running the renderPlaceholder pipeline (which runs renderRendering) will overwrite these // and we need to set them back how they were when we're done rendering the xBlock var originalRenderingDefinitionContext = RenderingContext.CurrentOrNull?.PageContext?.PageDefinition; try { // prevents editing the snippet in context, so you cannot mistakenly change something shared by mistake if (Context.PageMode.IsExperienceEditorEditing) { Context.Site.SetDisplayMode(DisplayMode.Preview, DisplayModeDuration.Temporary); } var pageDef = new PageDefinition { Renderings = new List <Rendering>() }; //Extracts the item's layout XML, then parses all of the renderings out of it. pageDef.Renderings.AddRange(GetRenderings(GetLayoutFromItem())); // Uncovers the main layout rendering var pageRenderingArgs = new GetPageRenderingArgs(pageDef); PipelineService.Get().RunPipeline("mvc.getPageRendering", pageRenderingArgs); //Renders all placeholders for the layout rendering, which would be the entire page var renderPlaceholderArgs = new RenderPlaceholderArgs(PerformItemRendering.ItemRenderingKey, writer, pageRenderingArgs.Result) { PageContext = new PageContext { PageDefinition = pageDef } }; using (PageRenderItemDefinitionContext.Enter(new PageRenderItemDefinitionContext(pageDef, Item, originalDisplayMode))) { PipelineService.Get().RunPipeline("mvc.renderPlaceholder", renderPlaceholderArgs); } } catch (Exception e) { Log.Error("There was a problem rendering an item to string", e, this); if (originalDisplayMode == DisplayMode.Edit || originalDisplayMode == DisplayMode.Preview) { writer.Write($"<p class=\"edit-only\">Error occurred while rendering {Item.Paths.FullPath}: {e.Message}<br>For error details, <a href=\"{LinkManager.GetItemUrl(Item)}\" onclick=\"window.open(this.href); return false;\">visit the target page</a></p>"); } } finally { // replace the renderings in the current context with the ones that existed before we ran our sideline renderPlaceholder // because they have been overwritten with the xBlock's renderings at this point if (originalRenderingDefinitionContext != null) { RenderingContext.CurrentOrNull.PageContext.PageDefinition = originalRenderingDefinitionContext; } Context.Site.SetDisplayMode(originalDisplayMode, DisplayModeDuration.Temporary); } }