Home > Templates > Razor Syntax Support

Razor Syntax Support

We introduced the Text Template Engine in previous posts such as this one, or this one; and both posts feature some quick examples of how to use the default syntax to create templates:

  • use [%= foo %] to print the value of the foo variable,
  • use [% foreach (string value in values) %] to embed C# statements.

Starting with the next CodeFluent Entities build (> 50401.587), the CodeFluent Template Engine now supports Razor.

Razor, is basically a new view engine for ASP.NET MVC developed by Microsoft and shipped with ASP.NET MVC 3. Basically, the main goal of Razor is that it’s compact, expressive and fluid. To quote Scott Guthrie (view source):

Razor minimizes the number of characters and keystrokes required in a file, and enables a fast, fluid coding workflow. Unlike most template syntaxes, you do not need to interrupt your coding to explicitly denote server blocks within your HTML. The parser is smart enough to infer this from your code. This enables a really compact and expressive syntax which is clean, fast and fun to type.

To generate files from templates using the Razor syntax you need:

  • To be in .NET 4,
  • References to the CodeFluent.Runtime.dll and CodeFluent.Model.Common.dll (versions strictly upper than 50401.587),
  • A reference to System.Web.Razor

As an example, here’s a template which loads from its context a string array containing all NBA teams, and lists them in a text file:

@ template engine=”razor”

Generated @System.DateTime.Now

    string[] nbaTeams = (string[])Context[“NbaTeams”];

Current NBA Teams:
@foreach (string team in nbaTeams) {
    <text>- @team</text>

Check-out this nice blog post for a quick reference on the Razor syntax.

Then here’s the code using CodeFluent’s Template Engine which runs the template and prints its output on the console:

class Program
    private static string[] _nbaTeams = { /* All Teams...*/ };

    static void Main(string[] args)
        Template template = new Template();
        template.Load("Sample.txt", TemplateOptions.Default);
        Hashtable context = new Hashtable();
        context["NbaTeams"] = _nbaTeams;
        template.Run(Console.Out, context);

Here’s the output:

Generated 04/12/2011 14:42:13

Current NBA Teams:
        – Boston Celtics
        – New Jersey Nets
        – New York Knicks
        – Philadelphia 76ers
        – Toronto Raptors
        – Dallas Mavericks
        – Houston Rockets
        – Memphis Grizzlies
        – New Orleans Hornets
        – San Antonio Spurs
        – Chicago Bulls
        – Cleveland Cavaliers
        – Detroit Pistons
        – Indiana Pacers
        – Milwaukee Bucks
        – Denver Nuggets
        – Minnoseta Timberwolves
        – Portland Trail Blazers
        – Oklahoma City Thunder
        – Utah Jazz
        – Atlanta Hawks
        – Charlotte Bobcats
        – Miami Heat
        – Orlando Magic
        – Washington Wizards
        – Golden State Warriors
        – Los Angeles Clippers
        – Los Angeles Lakers
        – Phoenix Suns
        – Sacramento Kings

Categories: Templates Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

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