Using Synthesis with Blade

Kam Figy edited this page May 30, 2013 · 1 revision

Synthesis features neat integration with Blade, a Sitecore presentation framework. This integration automates the conversion of Synthesis types into available Blade model types without defining an explicit Blade presenter.

For Example

What the heck does that last paragraph even say? It means you can define Blade views like this (.cshtml example):

@inherits RazorRendering<MyProject.Model.ISamplePageItem>

When the Synthesis integration sees this, it automatically performs a conversion of the rendering's data source into the requested Synthesis type and returns it as the model. This enables you to quickly write renderings that have no idea Synthesis even exists!

Going Nuts

Sitecore 7 introduces the idea of using index queries and Sitecore query expressions as rendering data sources. This means you may receive more than one item as a data source. Blade fully supports this kind of scenario with ListPresenters. The Synthesis integration takes this a step further by automatically enabling list presenters with Synthesis objects. Check this out:

@inherits RazorRendering<IEnumerable<MyProject.Model.ISamplePageItem>>

It's that easy - make your model an IEnumerable<T> of a Synthesis type and you will get an enumerable set of data sources provided to you. It's fine to use this if you might get a single item as well; the enumerable will have one element. Note: at present only IEnumerable is supported, not derivatives of IEnumerable such as arrays or lists

Performance note: Currently, the multiple data source logic involves converting index items into Sitecore.Data.Item so this solution is appropriate for relatively small result sets (< 100 items) and/or with output caching. Once Sitecore 7 Update 2 launches, there will be an optimization available to pass through mapped index objects and eliminate this warning.

Razor Extensions

Synthesis also extends Blade's razor HTML helpers with some helpers for rendering Synthesis field types:

@Html.DateTimeFor(x => x.DateField)
@Html.ImageFor(x => x.ImageField)
@Html.FileLinkFor(x => x.FileField)
@Html.TextFor(x => x.TextField)
@Html.HyperlinkFor(x => x.LinkField)

These work as the xxxFor() form helpers in MVC, only for Sitecore field types. Most of them also have additional overloads that allow setting CSS classes, tweaking image width and height properties, setting page edit-ability, etc. HTML encoding is handled automatically by the helpers and Page Editor is fully supported.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.