LibrarySites.Banner

Processing Placeholders with MVC in the Sitecore ASP.NET CMS

This blog post explains how the Sitecore ASP.NET web Content Management System (CMS) processes placeholders. For more information about using MVC with Sitecore, Posts about Using MVC with the Sitecore ASP.NET CMS.

Processing placeholders in MVC (Razor) views is not much different than processing placeholders in layouts and sublayouts, but with a few key differences:

  • Syntax: Rather than using a Web Control to represent placeholders as you would with Web Forms, in MVC you use an HTML Helper method.
  • Implementation: Sitecore uses the mvc.renderPlaceholder pipeline to render MVC placeholders. For more information about pipelines, see the blog post All About Pipelines in the Sitecore ASP.NET CMS.

To render a placeholder in an MVC view, you can pass the placeholder key to the Placeholder() method in the Sitecore.Mvc.Helpers.SitecoreHelper object returned by calling the Sitecore() method of the System.Web.Mvc.HtmlHelper class exposed by the Html property of the System.Web.Mvc.WebViewPage class from which the view inherits (or the System.Web.Mvc.WebViewPage<TModel> class for strongly-typed views). Put another way, to render the placeholder with key "body", put this in your view:

@Html.Sitecore().PlaceHolder("body")

To render the contents of a placeholder, Sitecore invokes the mvc.renderPlaceholder pipeline defined in the /App_Config/Include/Sitecore.Mvc.config file. By default, the mvc.renderPlaceholder pipeline consists of the following processors:

  • EnterPlaceHolderContext: This processor enters a Sitecore.Mvc.Presentation.PlaceHolderContext instance that tracks placeholder processing, supporting nested placeholders. The static property named Current in the PlaceHolderContext class exposes the current PlaceHolderContext.
  • AddWrapper (if you have enabled /App_Config/Include/Sitecore.MvcExperienceEditor.config): This processor adds Experience Editor (formerly Page Editor) features to the placeholder depending on the mode in which the user accesses the page.
  • PerformRendering: This processor invokes the new mvc.renderRendering pipeline for each relevant rendering. Technically, this iterates the renderings associated with the appropriate device and placeholder key in the Sitecore.Mvc.Presentation.PageDefinition object associated with the current Sitecore.Mvc.Presentation.PageContext. More about the mvc.renderRendering pipeline and the these classes in a future blog post, but for now you can think of the mvc.renderRendering pipeline as invoking a rendering, the PageDefinition class as the list of all renderings defined in layout details for all devices for the context item, and the PageContext as containing information about the current MVC request.

You might notice that there is no ExitPlaceHolderContext processor in the mvc.renderPlaceholder pipeline. The reason is that the EnterPlaceHolderContext processor adds the PlaceHolderContext object it creates to a Disposables property of the Disposables collection property of the of the Sitecore.Mvc.Pipelines.Response.RenderPlaceHolder.RenderPlaceHolderArgs argument passed through the pipeline, which inherits from the Sitecore.Mvc.Pipelines.MvcPipelineArgs class used for Sitecore MVC pipelines. The MvcPipelineArgs class implements the System.IDisposable interface and in its Dispose() method, which calls the Dispose methods in any disposable objects in the Disposables collection.