-
Notifications
You must be signed in to change notification settings - Fork 4
Using Razor
Blade includes rich support for the Razor (v2) templating language. It's important to note that Blade's implementation of Razor is very close to ASP.NET MVC because it uses the MVC APIs to render. MVC conventions such as HTML helpers, form renderings, and model binding are all relevant.
Blade adds a Razor rendering type to Sitecore. You can use Blade renderings just like Sublayouts, WebControls, or XSLTs. Important limitation: You cannot use Razor for layouts, nor can Razor renderings contain placeholders.
Unlike the Sitecore 6.6+ MVC implementation, Blade's Razor renderings are fully compatible with the Sitecore Web Forms layout engine, and work with existing non-MVC-aware modules such as Web Forms For Marketers.
- Install Blade as directed in the installation instructions.
- Create a Razor file in your visual studio project. This can be automated by installing the Visual Studio templates distributed with Blade in the
Documentation
folder, or you can insert a text file with a .cshtml extension.- The Razor file must declare a model type as its first line, like so:
@model Type.Of.ViewModel
- The Razor file must declare a model type as its first line, like so:
- Create a Razor rendering item in Sitecore, and enter the virtual path (e.g.
~/layouts/test.cshtml
) to the Razor file in itsView path
field. This is very similar to adding a sublayout. - Attach the razor rendering to an item's layout details as you would any other rendering. Note: Page designing is fully supported for Razor renderings.
In terms of getting a model object, the Razor support operates exactly the same as the WebControl/UserControl Views - the model is resolved using the presenter factory and presented as the Model to the Razor page. If it is configured correctly, a few helpers are available to use:
@Html.Partial("partials/foo") // works as an MVC partial would, supports relative paths and passing models
@Html.Raw("<script>alert('hax0rd!');</script>") // as in MVC
If using Blade with Synthesis, Synthesis adds additional helpers to simplify rendering properties from Synthesis models.
You can also use Razor templates within WebForms-based pages to build statically bound "controls." This is accomplished by using a WebControl shim that knows how to load a Razor template internally. Simply register the Blade.Views
namespace as you would any other WebControl namespace and use the control like so:
<blade:RazorTemplate runat="server" Path="~/full/path/to/template.cshtml" />
You can also pass models to these templates using their Model
property. Unlike partials in a fully Razor world, these statically bound templates do not inherit any models from their parent rendering (which may not even have a model), so by default their model is null unless explicitly set.
Statically bound Razor templates also do not support relative paths, and require a full virtual path to the template including .cshtml.
Blade's Razor views fully support output caching. This should be configured on the rendering item in Sitecore as there is no programmatic access to the cache parameters from within the cshtml. There is not currently any method of generating a custom cache key for Razor templates, however the default CachingID (the view path) should be sufficient for average usage.
Razor v2 syntax is now supported (ala ASP.NET MVC 4). See these posts to read about what's new in Razor 2.