Layouts with Razor Engine dll

Razor engine is a library that you can use to generate your cshtml files. For example you can use it for generating emails. It gives you the ability to add a layout (Masterpage) and then generate your templates based on the Layout.You can easily add it by nuget (add library package reference) .

using (var service = new TemplateService())
       {
var style = "<style type=\"text/css\">hr {color:sienna;}p {margin-left:20px;}</style>";
var layoutTemplate = style + "<h1>Main</h1>@RenderSection(\"Child1\")@RenderSection(\"Child2\")";
var childTemplate = "@{ _Layout =  \"Parent\"; }@section Child1 {<p>child1 </p>}@section Child2 {<p>child2 deatils <a href=\"http://www.google.com\">Google</a></p>}";
service.Compile(layoutTemplate, null, "Parent");
var result = service.Parse(childTemplate);
       }



You can download sample from Here:
https://github.com/Azadehkhojandi/RazorEngineTest

Resources:
https://github.com/Antaris/RazorEngine

Comments

Anonymous said…
Your example doesn't work. are you able to update?
Azadeh Khojandi said…
try to down load it from :
https://github.com/Azadehkhojandi/RazorEngineTest/downloads

What's the version of your visual studio ?

It works fine with
VS2010 and RazorEngine.3.0.8

Check infrastructure project
Anonymous said…
Hi, thanks for your reply,

Your test project [https://github.com/Azadehkhojandi/RazorEngineTest/downloads] works fine actually (VS11 here), the issue i had seemed to relate to version 3.0.0, where _Layout could not be resolved, additionally, the signature for TemplateService.Parse was different. This lead to a bit of confusion my side on which version of the RazorEngine i should use. (also, there is a System.Web.Razor namespace nowadays, so still somewhat confused on what i should be using, do you have any thoughts on this?)

Thanks again for your post, there doesn't appear to be a lot of information out there with many working examples at this time, so your example is hugely appreciated.

Cheers
Azadeh Khojandi said…
if you are using mvc 4.0 instead of mvc 3.0. you may need to look at the following link
https://github.com/Antaris/RazorEngine/issues/71
Azadeh Khojandi said…
Why do you need to use razor engine dll (https://github.com/Antaris/RazorEngine)?
do you want it for generating email?
Anonymous said…
Hiya,

Was looking at the Razor Engine as a more flexible alternative to the built-in ASP.NET MVC Razor implementation (which is effectively bound to a Controller's Actions).

I was just after a more generic templating solution that would work outside of MVC, handle partial views, and run in a similar fashion to how other templating frameworks operate. (django, mustache, ejs/jade, etc).

And yup, email generation is one of the things i plan to use it for :D
Azadeh Khojandi said…
I want to share my personal experience. using razor engine dll to parse layouts and partials kills the machine.
you should be careful an do a proper load testing before pushing into live site.
It might be helpful if you look at Nvelocity (http://docs.castleproject.org/MonoRail.NVelocity.ashx).
Anonymous said…
Hi, thanks for the heads up.

I typically cache template output, but for non cached templates (where dynamic or real-time data passes through), i do compile them first rather than compile and parse from scratch each time. The compile step is expensive after all.

So my compiled template(s) exist for the lifetime of the application, and with this, I get rough throughput of about 80k parses per second for a basic template with a partial render (@section, i.e) and some dynamic data.

Are you saying that you have noticed issues with performance even after compilation?

Cheers mate
Azadeh Khojandi said…
I had problem with concurrent calls when I was using compile and parse for dynamic and real-time data.
I recommend to use razor engine dll asynchronously.if you want to use it like the following you may have problems for concurrent calls.

(1)get user registration details then (2)send email wait for razor dll to compile and parse the template with data and then (3)redirect to thank you page.

I suggest

(1)get user registration details then (2) call send message method ,add sending message with registration data in queue (you can use RabbitMQ) (3)redirect to thank you page