Skip to content
Ivan Balan edited this page Aug 7, 2016 · 7 revisions

Layout page

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.

Example

  • 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.

Clone this wiki locally