2

I am creating a custom placeholder, that always renders the contents of a placeholder on a specific Sitecore item, no matter what page the custom placeholder is added to.

I have been using this blot post as inspiration and is currently stuck trying to get the pipeline invoked using the following code:

public class PerformRendering : Sitecore.Mvc.Pipelines.Response.RenderPlaceholder.PerformRendering
{
    public const string RenderPlaceholderTypeKey = "rt";
    protected override void Render(string placeholderName, TextWriter writer, RenderPlaceholderArgs args)
    {
        if (!args.CustomData.ContainsKey(RenderPlaceholderTypeKey))
        {
            base.Render(placeholderName, writer, args);
            return;
        }

        var renderer = (IRenderPlaceHolder)ReflectionUtil.CreateObject((Type)args.CustomData[RenderPlaceholderTypeKey]);
        Assert.IsNotNull(renderer, "Could not instantiate custom placeholder renderer for placeholder " + placeholderName);
        renderer.Render(writer, GetRenderings(placeholderName, args));
    }
}

 public void Render(TextWriter writer, IEnumerable<Rendering> renderings)
    {
        try
        {

                RenderingReference[] footerRenderings = ContextSettingsItem.Visualization.GetRenderings(Sitecore.Context.Device, true);
                List<Rendering> renderingsToRender = new List<Rendering>();
                renderingsToRender.AddRange(footerRenderings.Select(reference => new Rendering()
                {
                    RenderingItemPath = reference.RenderingID.ToString(),
                    Parameters = new RenderingParameters(reference.Settings.Parameters),
                    DataSource = reference.Settings.DataSource
                }));

                writer.Write("<div>");
                foreach (Rendering rendering in renderingsToRender)
                {
                    PipelineService.Get().RunPipeline<RenderRenderingArgs>("mvc.renderRendering", new RenderRenderingArgs(rendering, writer));
                }
                writer.Write("</div>");
            }
        }
        catch (Exception ex)
        {
            Sitecore.Diagnostics.Log.Info("Error loading footer item. ", ex.Message);
        }
    }

However, it seems that all renderings are just rendered in the order they are referenced and does not respect the rendering hiearchy (some renderings are in placeholders)

Can anyone help me understand how I can programmatically render the contents of a specific placeholder, from a specific item?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.