16

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?

casperOne
  • 73,706
  • 19
  • 184
  • 253
Ropstah
  • 17,538
  • 24
  • 120
  • 194
  • 3
    @casperOne: I appreciate the edit, however this has 'nothing' to do with MVC3. I rolled back because of this, however other edits (other than tags) are also rolled back... – Ropstah May 09 '11 at 15:08
  • 2
    @Ropstah: I see that now (didn't follow the link). You should specify that in the question somehow (i.e. "I'm using the Razor templating engine outside of ASP.NET MVC3" with "using the Razor templating engine outside of ASP.NET MVC3" linked) to make it clear to others (most will assume it is in ASP.NET MVC3. Please retain the code formatting; currently it's not clean and detracts from the question. – casperOne May 09 '11 at 15:17
  • 2
    @Ropstah: I just went ahead and did it. Should be clear as day now. – casperOne May 09 '11 at 15:20
  • 2
    Understand that any razor outside of mvc does not contain those helper functions. MVC3 specifically creates those for the RazorEngine. You can fairly easily add your own using the http://razor-engine.com by adding the layout template as part of a property of the base template. – Buildstarted May 09 '11 at 16:51
  • 1
    @casperOne: you're a star! :). @BuildStarted: I understand I need to create some stuff myself. The problem is I don't understand how to 'fetch' those method calls from a template and turn them into named sections or..? There are multiple 'hosts' or 'wrappers' for the engine, the link I posted worked in my situation so I'm using that one... Is the one at razor-engine.com different? – Ropstah May 10 '11 at 08:25
  • 1
    The razor-engine.com version is one of the examples linked to in the sample link you've provided. I thought you were talking about one. ( http://razorengine.codeplex.com ) – Buildstarted May 11 '11 at 20:26
  • I'm currently using 'your' razor engine. Are there any plans on implementing layouts at your side? – Ropstah May 16 '11 at 08:27
  • @Ropstah - v3 will support layouts, I'm hoping to finalise and push to Github and Nuget soon. https://github.com/Antaris/RazorEngine – Matthew Abbott Oct 22 '11 at 12:53

1 Answers1

6

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.

marto
  • 4,170
  • 1
  • 28
  • 39