RazorEngine, Templating Anywhere.

For the last couple of months I have been deep into building mobile WebApps with jQuery Mobile and MVC 4. I must say that working with MVC, as an old ASP hack, has been a very pleasant experience and change from WebForms. MVC really joins the flexibility and transparency of a scripted language, be it ASP or PHP, with a modern structured approach (maintainability, testability, readability etc).

Working with the View-engine you quickly get the feeling that this could be used for other things, such as generating stuff like SQL, MDX, HTML output for PDF conversion or other report generators.

Enter the CodePlex RazorEngine project, available right inside VS2010 via NuGet, which builds on MVC to deliver the same features for other .Net environments. Other people, like Rick Strahl, have made similar efforts to harness the power of Razor. This guy on CodeProject uses RazorEngine for CSS generation.

Having played with it for a couple of hours I discovered that the documentation and samples aren’t completely up to date, so here is my very simple code sample. I’m sure there’s a lot more to it, but this at least got me started:

using System;
using RazorEngine;
using System.Dynamic;

namespace BatchRazor
{
    public class Test {
        public string Name { get; set; }
    }

    class Program {
        public static void Main(string[] args)
        {
            string template =
                "@{var _v = \"Mr \" + Model.Name; }" +
                "Hello @_v, Welcome to Razor at @DateTime.Now.ToString()!";

            // Anonymous Console.WriteLine(Razor.Parse(template, new { Name = "World" }));

            // Dynamic dynamic d = new ExpandoObject();
            d.Name = "Expando1";

            Console.WriteLine(Razor.Parse(template, d));

            Razor.Compile(template, typeof(ExpandoObject), "complex");
            Console.WriteLine(Razor.Run("complex", d));

            dynamic e = new ExpandoObject();
            e.Name = "Expando2";
            Console.WriteLine(Razor.Run("complex", e));

            // Strongly typed var t = new Test() { Name = "Test" };
            Console.WriteLine(Razor.Parse(template, t));

            Razor.Compile(template, typeof(Test), "complex1");
            t.Name = "Test2";
            Console.WriteLine(Razor.Run("complex1", t));

            Console.ReadLine();
        }
    }
}
Advertisements
Posted in C#

One thought on “RazorEngine, Templating Anywhere.

  1. Matthew Abbott

    Nice article 🙂

    Just to let you know, I’m currently monitoring the new v3 beta release, which is essentially an aggressive redesign of most of the RazorEngine framework, and we’ve moved over to Github (https://github.com/Antaris/RazorEngine).

    I’m also in the process of writing both full documentation, and a migration guide for anybody wanting to transition from v2.1 -> v3.

    The v3 beta is currently available on Nuget also (Install-Package RazorEngine).

    Happy coding 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s