Sunday, 20 May 2012

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
Post a Comment