Quick Start Guide

To get started using the Razor Templating Engine, add a reference to RazorEngine.dll to your project (it requires System.Web.Razor.dll, included in the release). Once you've added the reference, you can start parsing templates.

Simple Template Parsing

  string template = "Hello @Model.Name! Welcome to Razor!"
  string result = Razor.Parse(template, new { Name = "World" });

In the above example, I am using an anonymous object to populate the Model property of the base template.

Inline Helper Support

  string template = 
  @"@helper MyMethod(string name) {
      Hello @name
  @MyMethod(Model.Name)! Welcome to Razor!";
  string result = Razor.Parse(template, new { Name = "World" });

Inline helpers are particularly useful for repeating complex subtemplates within a large template.

Precompiling Templates

You can precompile your templates before use if you have any really nontrivial views to compile.
    string template = "Some really complex template that will take time to parse";
    Razor.Compile(template, "complex");

If your template requires a model you can pass in your model type or, if your type is anonymous, you can call the CompileWithAnonymous() method. It is not necessary to pass in your Anonymous type.
    Razor.Compile(template, typeof(SomeModel), "complex");
    Razor.CompileWithAnonymous(template, "complex");

Last edited Mar 7, 2011 at 2:24 PM by BuildStarted, version 8


faib920 Nov 27, 2015 at 2:52 AM 
How to include an external file? which defines some common functions.

jalalx Apr 30, 2012 at 8:39 AM 
is possible to use razor in desktop applications for reporting reasons without referencing to any system.web assembly?

gichamba Jan 30, 2012 at 7:35 PM 
How do you determine whether a compiled template already exist in the cache?

mikechenx Aug 8, 2011 at 3:58 AM 
It doesn't work in VB.NET. The error message is as below:
Unable to compile template. Check the Errors list for details.

Source code:

Dim template = "Hello @Model.Name! Welcome to Razor!"
Dim myEmp2 = New With {.Name = "World"}
Console.WriteLine(Razor.Parse(template, myEmp2))

manit Aug 4, 2011 at 9:43 PM 
this is great, so far really easy to use. and it is fast.