-
Notifications
You must be signed in to change notification settings - Fork 4
Blade Rendering Lifecycle
Blade uses the Model-view-presenter pattern to make Sitecore renderings feel like MVC, give you the control of MVC, but still fit within the normal Sitecore layout engine paradigms.
- The Sitecore layout engine processes the layout details and selects the renderings to use (.cs, .cshtml, or .ascx)
- The rendering, or View in Blade terms, knows what kind of ViewModel object it expects to get (via inheriting a generic Blade view class). The ViewModel may be any .NET type.
- The View contacts the current Presenter Factory and requests a Presenter that is capable of delivering the ViewModel type the View needs to render. Presenters, in ASP.NET MVC terms, can be thought of as a single-action controller class - however unlike MVC they only produce the model, as the view is in front of them in the lifecycle.
- The Presenter Factory resolves a Presenter, or depending on the factory implementation may use a Default Presenter that is fallen back to if no explicit presenter exists (e.g. Synthesis.Blade uses a default presenter that implicitly binds the context item to Synthesis item types without needing an explicit presenter for each type).
- The Presenter resolves the ViewModel using whatever logic it has defined within it. The Presenter is a user-defined class that derives from IPresenter (or more likely a class that derives from IPresenter, such as SitecorePresenter or SynthesisPresenter that include extra niceties).
- The View is provided the ViewModel from the Presenter and uses the values within to render itself
This example uses the Synthesis Presenter from Synthesis.Blade in a WebControl rendering to display an item whose template is My Template.
public class MyView : WebControlView<IMyTemplateItem>
{
public void RenderModel(HtmlTextWriter writer)
{
writer.Write(Model.Title.RenderedValue);
}
}
This example uses a custom ViewModel and Presenter class
public class CustomViewModel
{
public TestProperty { get; set; }
}
// note: if using Synthesis.Blade, use SynthesisPresenter here instead to get passed a typed item instead of Item
public class CustomPresenter : SitecorePresenter<CustomViewModel>
{
protected override CustomViewModel GetModel(Blade.IView view, Item entity)
{
var model = new CustomViewModel();
model.TestProperty = entity.DisplayName;
return model;
}
}
public class MyView : WebControlView<CustomViewModel>
{
public void RenderModel(HtmlTextWriter writer)
{
writer.Write(Model.TestProperty);
}
}
List presenters are a special type of presenter that allow you to take advantage of the same support that a SitecorePresenter
does, but for renderings that have multiple items possible as their data source, for example a Lucene query data source in Sitecore 7, or a Sitecore Query based data source like query:./Articles/*
. These expose a similar interface to SitecorePresenter
but instead of an Item
you get an Item[]
parameter.