Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Razor engine - Implementing layouts

Tags:

.net

razor

I'm using the following snippet to enable Razor templating in my solution (outside of ASP.NET MVC3). Is it possible to easily implement layouts?

Background info:

I'm at this point (templates are compiled into compiledTemplateAssembly):

var template = (RazorTemplateBase<TModel>) compiledTemplateAssembly.
    CreateInstance("RazorSpace." + entry.TemplateName + "Template");
template.Model = model;
template.Execute();
var output = template.Buffer.ToString();
template.Buffer.Clear();
return output;

I can imagine having a Layout property on my RazorTemplateBase class. But then? I understand that Html.Partial is a helper function which I can just implement to parse a template. But how do I parse those method calls renderBody() or renderSection() to accept other Razor views?

like image 327
Ropstah Avatar asked May 09 '11 12:05

Ropstah


People also ask

How do you use Razor layout?

Specifying a LayoutThe layout specified can use a full path (for example, /Pages/Shared/_Layout. cshtml or /Views/Shared/_Layout. cshtml ) or a partial name (example: _Layout ). When a partial name is provided, the Razor view engine searches for the layout file using its standard discovery process.

Is Razor pages MVC or MVVM?

The view model pattern is used extensively in MVC application development, where it mainly represents data, but typically little behaviour. In Razor Pages, the PageModel is also the view model. Razor Pages is sometimes described as implementing the MVVM (Model, View ViewModel) pattern.

Is Razor better than MVC?

From the docsdocsMicrosoft Docs is the library of technical documentation for end users, developers, and IT professionals who work with Microsoft products.https://en.wikipedia.org › wiki › Microsoft_DocsMicrosoft Docs - Wikipedia, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

Do Razor pages replace MVC?

A Razor Page is almost the same as ASP.NET MVC's view component. It has basically the syntax and functionality same as MVC. The basic difference between Razor pages and MVC is that the model and controller code is also added within the Razor Page itself. You do not need to add code separately.

What is layout Cshtml?

cshtml represents the layout of each page in the application. Right-click on the Shared folder in the Solution Explorer, then go to Add item and click View. Copy the following layout code.

Can you mix MVC and Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.


1 Answers

I'm currently working on something very similar. It is a front end templating framework based on Nancy. I extended the Nancy's Razor implementation by Phil Haack. I have managed to get Partials, Templated Helpers and Layouts working.

To render the layout I have a Layout property and inside the layout I have a content placeholder "{{content}}". So when I render the view if the Layout property is set I render the layout and then replace the content placeholder.

The project is called Appia. Have a look at the sample views.

Here is my baseView implementationbaseView implementation and here is the view engine code. It borrows a lot from the MVC Razor implementation and also has some Nancy specific stuff but it shouldn't be too hard to figure out what's happening.

like image 135
marto Avatar answered Oct 02 '22 19:10

marto