Official T4 team blog

T4 stands for Text Template Transformation Toolkit and is Microsoft's template based text generation framework included with Visual Studio.

T4 vs Razor – what’s the skinny?

T4 vs Razor – what’s the skinny?

  • Comments 4

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.

Digg This
  • 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?

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

  • 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

  • Ming,

    The answer is both are fully supported by Microsoft. Hopefully the article makes i tclear that they are optimized for slightly different purposes.

Page 1 of 1 (4 items)
Leave a Comment
  • Please add 5 and 6 and type the answer here:
  • Post