Skip to content

Examples

jlamfers edited this page Nov 17, 2012 · 5 revisions

When you start to use this framework then the most important class is the RazorMachine. The following examples (included at the examples project as well) use a local instance of RazorMachine. Please note that normally for performance reasons at any application you would create a singleton instance of RazorMachine (because of the inner instance bound JIT created type caching) and use that particular instance for all template executions.

Example 1 - Executing a template

RazorMachine rm = new RazorMachine();
ITemplate template = rm.ExecuteContent("Razor says: Hello @Model.FirstName @Model.LastName", 
  new {FirstName="John", LastName="Smith"});
Console.WriteLine(template.Result);

Example 2 - Executing a template using a layout

RazorMachine rm = new RazorMachine();
rm.RegisterTemplate("~/shared/_layout.cshtml", 
   "BEGIN TEMPLATE \r\n @RenderBody() \r\nEND TEMPLATE");
ITemplate template = rm.ExecuteContent("@{Layout=\"_layout\";} Razor says: Hello @Model.FirstName @Model.LastName", 
   new {FirstName="John", LastName="Smith"});
Console.WriteLine(template); // template.ToString() evaluates to template.Result 

Example 3 - Executing a template using a layout and _viewStart

RazorMachine rm = new RazorMachine();
rm.RegisterTemplate("~/shared/_layout.cshtml","BEGIN TEMPLATE \r\n @RenderBody() \r\nEND TEMPLATE"); 
rm.RegisterTemplate("~/_viewstart.cshtml","@{Layout=\"_layout\";}");
ITemplate template = rm.ExecuteContent("Razor says: Hello @Model.FirstName @Model.LastName", 
   new {FirstName="John", LastName="Smith"});
Console.WriteLine(template); // same result as example 2 

Example 4 - Executing a template by a virtual path using a layout and _viewStart

RazorMachine rm = new RazorMachine();

rm.RegisterTemplate("~/shared/_layout.cshtml","BEGIN TEMPLATE \r\n @RenderBody() \r\nEND TEMPLATE"); 
rm.RegisterTemplate("~/_viewstart.cshtml","@{Layout=\"_layout\";}");
rm.RegisterTemplate("~/simpleTemplate.cshtml","Razor says: Hello @Model.FirstName @Model.LastName"); 

ITemplate template = rm.ExecuteUrl("~/simpleTemplate.cshtml", 
   new {FirstName="John", LastName="Smith"});
Console.WriteLine(template); // same result as example 2 

Example 5 - Returning information from anywhere (even from a layout) to the caller using the ViewBag

rm.RegisterTemplate("~/shared/_layout.cshtml", "@{ViewBag.PiValue=3.1415927;}");
rm.RegisterTemplate("~/_viewstart.cshtml", "@{Layout=\"_layout\";}");
ITemplate template = rm.ExecuteContent("Anything");
Console.WriteLine(template.ViewBag.PiValue); // => writes 3.1415927

Example 6 - Adding information from anywhere to a predefined ViewBag and returning it to the caller

RazorMachine rm = new RazorMachine();
rm.RegisterTemplate("~/shared/_layout.cshtml", "@{ViewBag.Values.Add(3.1415927);}");
rm.RegisterTemplate("~/_viewstart.cshtml", "@{Layout=\"_layout\";}");
ITemplate template = rm.ExecuteContent("Anything",
   viewbag:new {Values = new List<double>{0,1,2});
Console.WriteLine(template.ViewBag.Values[3]); // => writes 3.1415927 

Example 7 - Show generated code

RazorMachine rm = new RazorMachine(includeGeneratedSourceCode:true);
rm.RegisterTemplate("~/shared/_layout.cshtml", 
   "BEGIN TEMPLATE \r\n @RenderBody() \r\nEND TEMPLATE");
rm.RegisterTemplate("~/_viewstart.cshtml", 
   "@{Layout=\"_layout\";}");
ITemplate template = rm.ExecuteContent("Razor says: Hello @Model.FirstName @Model.LastName", 
   new { FirstName = "John", LastName = "Smith" });
Console.WriteLine(template); // writes output result
Console.WriteLine(template.GeneratedSourceCode); // writes generated source for template
Console.WriteLine(template.Childs[0].GeneratedSourceCode); // writes generated source for layout 

Example 8 - HTML encoding

RazorMachine rm = new RazorMachine();
// not encoded since all output is literal content. Literal content is never encoded.
Console.WriteLine(rm.ExecuteContent("Tom & Jerry").Result);

// encoded since the content is written as a string value
// and by default HtmlEncode is on
Console.WriteLine(rm.ExecuteContent("@Model.Text", 
   new {Text="Tom & Jerry"}).Result);

// not encoded since content is a written as a raw string
Console.WriteLine(rm.ExecuteContent("@Raw(Model.Text)", 
   new { Text = "Tom & Jerry" }).Result);

// not encoded since HtmlEncoding is turend off in code
Console.WriteLine(rm.ExecuteContent("@{HtmlEncode=false;} @Model.Text", 
   new { Text = "Tom & Jerry" }).Result);

rm = new RazorMachine(htmlEncode: false);
// not encoded since now html encoding if off by default, still you can set it on in code
Console.WriteLine(rm.ExecuteContent("@Model.Text", 
   new { Text = "Tom & Jerry" }).Result);

Example 9 - Root operator is resolved directly

RazorMachine rm = new RazorMachine(rootOperatorPath:"/MyAppName");
rm.RegisterTemplate("~/MyTemplate",
   "<a href='~/SomeLink'>Some Link</a>");
var template = rm.ExecuteUrl("/MyAppName/MyTemplate");
// same result as:
template = rm.ExecuteUrl("~/MyTemplate");
Console.WriteLine(template); // writes: <a href=/MyAppName/SomeLink>Some Link</a>

Example 10 - Razor 2: Attributes with null values are not rendered

RazorMachine rm = new RazorMachine();
var template = rm.ExecuteContent(@"<a href='~/SomeLink' 
    data-brand='@Model.Brand' 
    data-not-rendered='@Model.NullValue'>Some Link</a>", new {Brand="Toyota",NullValue=(string)null});
Console.WriteLine(template); // writes: <a href=/SomeLink data-brand='Toyota'>Some Link</a>

The previous examples examples register all content in memory before executing. Note that you can execute content directly from any resource like files and embedded resources using content providers. How this works is documented at CodeProject