-
Notifications
You must be signed in to change notification settings - Fork 258
Layout page
Diogo Osório edited this page Apr 28, 2017
·
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 template file.
In order to set a layout you must set a key of the layout page inside your template.
<div id="header">
<span>Some content here</span>
</div>
<div id="body">
<div>Hello John Doe</div>
</div>