A nested layout is any layout that refers to another, outer layout. This approach is useful when a subset of pages require the same markup, as is often the case when certain sections of a website need to appear slightly different from the others, yet retain the same general look and feel.
The first step to applying a nested layout to the Add Post page is to remove everything but its core content (everything inside the <body> tag). After this, use the same syntax as earlier examples to associate the page
with a layout, only instead of the _Layout.cshtml layout specify another (not yet created) layout file named ~/_AdminLayout.cshtml. The first few lines of the modified page are
shown below:
@{
Layout = "~/_AdminLayout.cshtml";
var title = Request["Title"];
var summary = Request["Summary"];
var body = Request["Body"];
...
}
Now it’s time to create the aforementioned _AdminLayout.cshtml layout. To do so, add a new CSHTML file named _AdminLayout.cshtml to the root of the website (follow the same steps used previously to create a new layout file). Then, completely clear the default template text, so that the file is empty, and replace with a reference to the _Layout.cshtml layout file as well as a call to the RenderBody() method. The following
snippet shows the full implementation of a nested layout: a Razor template that contains a call to the RenderBody() method and refers to another layout:
@{ Layout = "_Layout.cshtml"; }
@RenderBody()
if you attempt to execute the site with only this code in place, you will quickly find out that the original layout—_Layout.cshtml—is not satisfied: _AdminLayout.cshtml does not implement the required Header section!
a content view can define Razor sections that the view’s layout can access and execute. These section definitions, however, are only accessible to the immediate layout and vice-versa. In the case of the Add Post → Admin Layout → Main Site Layout scenario, this means that only the admin layout can access any sections defined in the Add Post content view. The main site layout cannot interact with the Add Post
content view at all! Conversely, the admin layout is responsible for implementing the required sections that the main site layout expects—it must explicitly implement these sections and cannot simply pass this responsibility to the Add Post content view.
The first step to applying a nested layout to the Add Post page is to remove everything but its core content (everything inside the <body> tag). After this, use the same syntax as earlier examples to associate the page
with a layout, only instead of the _Layout.cshtml layout specify another (not yet created) layout file named ~/_AdminLayout.cshtml. The first few lines of the modified page are
shown below:
@{
Layout = "~/_AdminLayout.cshtml";
var title = Request["Title"];
var summary = Request["Summary"];
var body = Request["Body"];
...
}
Now it’s time to create the aforementioned _AdminLayout.cshtml layout. To do so, add a new CSHTML file named _AdminLayout.cshtml to the root of the website (follow the same steps used previously to create a new layout file). Then, completely clear the default template text, so that the file is empty, and replace with a reference to the _Layout.cshtml layout file as well as a call to the RenderBody() method. The following
snippet shows the full implementation of a nested layout: a Razor template that contains a call to the RenderBody() method and refers to another layout:
@{ Layout = "_Layout.cshtml"; }
@RenderBody()
if you attempt to execute the site with only this code in place, you will quickly find out that the original layout—_Layout.cshtml—is not satisfied: _AdminLayout.cshtml does not implement the required Header section!
a content view can define Razor sections that the view’s layout can access and execute. These section definitions, however, are only accessible to the immediate layout and vice-versa. In the case of the Add Post → Admin Layout → Main Site Layout scenario, this means that only the admin layout can access any sections defined in the Add Post content view. The main site layout cannot interact with the Add Post
content view at all! Conversely, the admin layout is responsible for implementing the required sections that the main site layout expects—it must explicitly implement these sections and cannot simply pass this responsibility to the Add Post content view.
No comments:
Post a Comment