T4 Template Engine and Why I Love It
T4 Templates: The only thing that makes generating repetitive code fun!
If you don’t like writing boilerplate code, then T4 is about to be your new best friend.
This article shows how to use T4 to generate:
- HTML, PostScript, YAML, JSON, Python, and Golang files
- How to pass parameters into templates
- How to loop through arrays dynamically
A Brief History of T4
T4 Templates have been around since the Visual Studio 2005 era, hiding in the shadows while developers praised (or cursed) other tools. Microsoft introduced it as a code generation tool to automate tedious, repetitive code-writing tasks.
A quick timeline of T4’s history:
Release Year | Visual Studio Version | Notable Features |
---|---|---|
2005 | VS 2005 | First introduced |
2008 | VS 2008 | Improved syntax and debugging |
2010 | VS 2010 | Officially documented, better integration |
2012 | VS 2012 | Performance improvements |
2013 | VS 2013 | More Visual Studio integration |
2015 | VS 2015 | More performance optimizations |
2017 | VS 2017 | Continued support, but declining attention |
2019 | VS 2019 | Still works, but slowly becoming a legacy tool |
2022 | VS 2022 | Can be used, but alternatives like Source Generators are taking over |
T4 exists because repeating yourself is boring, and Microsoft wanted a way to generate code dynamically, especially for frameworks like Entity Framework, ASP.NET MVC, and Web API.
How Visual Studio Uses T4
If you’ve ever wondered, “Where is T4 hiding in Visual Studio?”, the answer is everywhere! Microsoft uses T4 for all sorts of code generation magic. Here are a few places:
- Entity Framework Models – When you generate
.edmx
files, T4 scripts generate the C# classes. - MVC Views (
.tt
files) – ASP.NET MVC scaffolding runs T4 scripts to create controllers and views. - Code Behind Files – Various Visual Studio extensions use T4 to generate
.designer.cs
files. - Custom Code Generators – Tools like CodeSmith and DevExpress leverage T4 for automation.
How to Use T4
There are two main ways to use T4 templates:
- Inside Visual Studio – Right-click a project → Add → Text Template (
.tt
). - Command Line – Run
TextTransform.exe
to process.tt
files outside Visual Studio.
How to Pass Parameters to T4
To use external input in T4, you define parameters using <#@ parameter #> directives.
We’ll use:
TemplateInputTextProvidedByTheCaller
– A string valueTemplateArrayOfStringsProvidedByCaller
– An array of strings
These values will be passed into all the templates.
Example: Generating C# Code with T4
Let’s say you need to generate multiple classes with boilerplate code. Instead of writing them manually (like a caveman), you can use T4.
Create a file called Models.tt
in your project and add this code:
|
|
When you save this .tt
file, Visual Studio automatically generates the C# code in Models.cs
:
|
|
No more copy-pasting code like a chump!
Running T4 from the Command Line
If you don’t want to use Visual Studio, you can run the T4 engine manually.
Open Command Prompt and run:
|
|
This will generate the same Models.cs
file.
Other Uses for T4
Besides C# code generation, T4 can be used for:
- SQL Scripts – Generate dynamic SQL statements.
- JavaScript – Create JS files dynamically based on backend logic.
- XML and JSON – Automate configurations.
- HTML & CSS – Generate frontend templates.
Generating a Static HTML Page
|
|
Generating a Weirdly Formatted PostScript File
|
|
Generating a YAML Configuration File
|
|
Generating an appsettings.json File
|
|
Generating a Python File with Embedded Strings
|
|
Generating a Go File with Embedded Strings
|
|
Key Points in the Above Samples
- T4 can accept parameters, making it flexible for automation.
- You can generate HTML, PostScript, YAML, JSON, Python, and Go files dynamically.
- Use
TextTransform.exe
to run T4 from the command line. - Loops allow T4 to iterate over arrays, making it super powerful for code generation.
Running These T4 Templates
To run T4 templates with parameters in Visual Studio:
- Add a
.tt
file to your project. - Define
TemplateInputTextProvidedByTheCaller
andTemplateArrayOfStringsProvidedByCaller
. - Save the file—Visual Studio will auto-generate the output.
For command-line execution, use:
|
|
Can You Run T4 on Linux?
Yes, but with some caveats.
T4 is natively supported in Windows through Visual Studio, but there’s no official support for it on Linux. However, there are alternatives that allow you to run T4 templates on Linux:
1. Using Mono’s t4
Command
The Mono Project provides a t4
command that works on Linux.
🔹 Install Mono
First, install Mono on your system:
Ubuntu/Debian:
|
|
Fedora:
|
|
Arch Linux:
|
|
🔹 Run T4 Templates
Once Mono is installed, you can use t4
to process templates:
|
|
This will generate the output file just like on Windows.
2. Using dotnet-t4
(A .NET Core Alternative)
If you’re working with .NET Core or .NET 5+, you can use dotnet-t4
, an open-source cross-platform T4 processor.
🔹 Install dotnet-t4
|
|
🔹 Run a T4 Template
|
|
This works on Linux, Mac, and Windows, making it a great cross-platform option.
3. Using Roslyn Source Generators (Modern Alternative)
Microsoft is phasing out T4 in favor of Source Generators, which work natively in .NET 5+.
If you’re starting a new .NET Core/.NET 5+ project, consider Source Generators instead of T4.
Learn more:
🔗 Roslyn Source Generators
References
Is T4 Dead?
Well… kind of. Microsoft has been moving toward Source Generators in .NET 5+, which are more powerful and performant. But T4 still works in Visual Studio 2022, and for quick-and-dirty code generation, it’s still king.
If you want something more modern, check out:
- Roslyn Source Generators – The fancy, official replacement.
- Scriban – A templating engine that doesn’t depend on Visual Studio.
- Razor Templates – If you’re already using ASP.NET Core.
Key Takeaways
- T4 is awesome for automating repetitive code generation.
- It’s built into Visual Studio and used for EF models, MVC views, and more.
- You can use it inside VS or from the command line.
- Microsoft is slowly replacing it with Roslyn Source Generators, but it’s still useful today.
- T4 is Windows-native but works on Linux with Mono (
t4
) ordotnet-t4
. - Mono’s
t4
command is the easiest way to run T4 on Linux. dotnet-t4
is a cross-platform alternative for .NET Core/.NET 5+.- For modern .NET development, consider Roslyn Source Generators.