-
Notifications
You must be signed in to change notification settings - Fork 260
Layout page
Ivan Balan edited this page Aug 7, 2016
·
7 revisions
Your template may contain common parts which remains the same throughout other templates such as the header, left bar, right bar or footer section. RazorLight supports a concept of a Layout view which contains these common UI parts, so that you don't have to write the same code in every page. The layout view is same as the master page of the ASP.NET webform application.
- Create a layout page
<div id="header">
<span>Some content here</span>
</div>
<div id="body">
@RenderBody()
</div>
- Create a view
@model MyTestViewModel
@{
Layout = "_Layout.cshtml"
}
<div>Hello @Model.Name</div>
- Initialize a RazorLight engine for physical files via EngineFactory
var engine = EngineFactory.CreatePhysical(@"C:/path/to/views/root/folder");
var model = new MyTestViewModel()
{
Name = "John Doe"
};
string result = engine.Parse("Test.cshtml", model);
//or
string result = engine.Parse("folder1/Test.cshtml", model)
engine.Parse()
takes 2 parameters - a key of the template and the model. While parsing physical files, a key - is a relative path to your view file.
In order to set a layout for your view - you must set key of the layout page at the top of your view.