Share via


T4 vs Razor – what’s the skinny?

You might have noticed in the recent storm of Web Tools releases that there’s a groovy new kid on the template engine block – Razor.

A few customers have been asking folks on both teams some questions around this, such as the following.

So the T4 and Razor teams got ourselves together in a room and came up with a common way to express how we think about some of these things. Here’s what we came up with:

  T4 Razor
Design-time hosting Visual Studio & co-installed tools Anywhere
Runtime options Preprocessed templates Rehost Razor engine
Recommended for generating which file types? Not markup Markup
Optimized for Generating arbitrary code assets Efficient HTML generation
Productivity aids Class feature blocks  (virtual methods) Include directive (template fragment libraries) Helpers (static standalone code) Sections (overridable content)
Extensibility Custom directives VS Service Custom host Customize or replace most elements of the stack Custom host
Editing 3rd party editors VS built-in (HTML output optimized)
Core control languages C#/VB C#/VB

 

In short, we have two engines ‘cos they’re optimized for different use cases.

  • Razor uses its implicit knowledge of the type of output it’s generating to allow it to disambiguate control code from output markup and so use a much terser and more pleasing syntax.
  • T4 is designed to generate arbitrary text output, which often means that  the output you’re generating and the control language will be the same; consequently, it has to use more explicit demarcation syntax.

We’ll keep adding individual syntax and features that make sense for those separate use cases, but we’ll try and avoid being different at a conceptual level just for the sake of it.

Technorati Tags: T4,Razor,Visual Studio,Template Engines

Digg This

Comments

  • Anonymous
    March 25, 2011
    Thank you, this is a useful and interesting blog post. I am curious, in theory at least could Razor syntax have been implimented as a T4 template?

  • Anonymous
    April 15, 2011
    Good article, but i want to ask, which one is main stream & support by microsoft dev team?

  • Anonymous
    April 17, 2011
    IMO from the table it looks like Razor and T4 is used for different things. If you need a general tool for generating code and other text assets ==> T4 If you need a tool optimized for generating HTML content ==> Razor So my advice would be to ask yourself : What should the tool help you with? Then pick the tool most valuable to you. Regards, Mårten

  • Anonymous
    May 02, 2011
    Ming, The answer is both are fully supported by Microsoft. Hopefully the article makes i tclear that they are optimized for slightly different purposes.