开发者

.NET Razor engine - Implementing layouts

开发者 https://www.devze.com 2023-03-04 03:46 出处:网络
I\'m using the following snippet to enable Razor templating in开发者_如何学Go my solution (outside of ASP.NET MVC3). Is it possible to easily implement layouts?

I'm using the following snippet to enable Razor templating in开发者_如何学Go 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消