Questions tagged [t4]

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

T4 is used by developers to automate repetitive text file creation processes. Text files can be anything from HTML code, XML, XAML and most often compilable code files (Visual Basic, C# or other languages).

T4 uses a custom template format which can contain C# or Visual Basic executable code along with string literals that become part of the generated file.

Advantages

  1. Visual Studio integration

    Whenever a developer creates and saves a file with extension *.tt, Visual studio automatically runs the T4 templating engine and processes the saved template. There's no need to run any external tool which makes it transparent for developers to use.

  2. .NET language support

    It's easier for developers to use something that they know rather than reinventing the wheel. Developing T4 templates is very similar to writing ASPX files where executable code sits along string literals (HTML code interpreted by the client browser) and manipulates it as required.

Disadvantages

  1. Lack of Visual Studio tooling support

    It's possible to write and save T4 templates inside Visual Studio but developers won't have any other advantages of a modern development environment like code formatting/colouring or code IntelliSense. Third-party tools are required to provide this functionality. Available extensions include Tangible T4 Editor and Devart T4 Editor among others.

  2. One template, one output file

    It's sometimes desired for a template to process several resources (either files, database tables, etc.) at once and output just as many text files. By default, T4 doesn't support multiple file output. But there are solutions to overcome this limitation as well.

Usage

There are lots of different usage scenarios for T4 templates that can greatly benefit developers to shorten their development time. There are templates on the internet for:

Microsoft also uses T4 templates for ASP.NET MVC controller and view generation when developers add a new Controller or View from within Visual Studio.

1602 questions
15
votes
2 answers

Executing a T4 text template in Visual Studio Code

I created a T4 text template (.tt) file in Visual Studio Code, but unlike Visual Studio 2017 (or 2015 ,...) it won't generate the output file after saving the .tt file. How can I generate the output in Visual Studio Code?
theGhostN
  • 342
  • 7
  • 20
15
votes
1 answer

How to use T4 to generate two files at the same time from one template?

I am having a case in which I need to generate two CSharp code files with pretty the same code but different namespace of the input and output types of the methods. In fact each file is for a specific country and the types come from country specific…
Rez.Net
  • 1,354
  • 2
  • 19
  • 28
15
votes
2 answers

Adding [DataMember] [DataContract] attributes in Entity Framework POCO Template

I would like some help adding in a POCO .tt Entity Framework template Attributes to support WCF serialization and if its possible how to add namespace usings to each entity. Thank you.
George Taskos
  • 8,324
  • 18
  • 82
  • 147
15
votes
3 answers

Referencing an assembly in a T4 template

It's been a while since I last used T4 and this is probably a silly question... Is it possible to reference an arbitrary assembly from a template? Example: I have a class that I'd like to use in Project X Project X.Test references X and contains…
Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
15
votes
2 answers

Ascertaining Project Directory from T4 Templates

I am using T4 templates in Visual Studio for code generation, and am trying to reference a local .xml file from the template code so I can parse it for metadata about the application's data model. When the template code (which runs at compile time…
blaster
  • 8,876
  • 11
  • 48
  • 77
15
votes
3 answers

How do you use .net Reflection with T4?

I have a c# project which includes a Text Template. I would like this template to generate some SQL based on reflecting against the C# classes in the project. How does one access the current project's contents using T4? Is it possible, and if so, is…
Andrew Theken
  • 3,392
  • 1
  • 31
  • 54
14
votes
3 answers

T4 templates error: loading the include file ef.utility.cs.ttinclude returned a null or empty string

I have overridden the controller generation T4 templates (ControllerWithContext.tt) as described here. I would like to take advantage of the code helper utilities found in EF.utility.CS.ttinclude as used in the POCO model generator T4 template. …
Mark_Gibson
  • 922
  • 1
  • 12
  • 32
14
votes
4 answers

How to get t4 files to build in visual studio?

When I build my c# solution the .tt files will not create the .cs file outputs. But if I right click the .tt files one at a time in solution explorer and select "Run Custom Tool" the .cs is generated, so the build tool setting is correct. What do I…
P a u l
  • 7,805
  • 15
  • 59
  • 92
14
votes
4 answers

T4 Toolbox - Referencing Class in Current Assembly

I am writing a T4 script which reflects over certain classes and provides code generation based on them. The problem is that my script errors out, saying that the classes in my current project cannot be accessed. The script itself resides in the…
Nicko-Mctricko
  • 461
  • 5
  • 6
14
votes
3 answers

T4 Toolbox - mixing class feature and statement blocks

I'm a T4 newbie trying to use T4 Toolbox to generate F# code based on this answer, but it seems that class feature blocks can't be mixed with statement blocks. Here's my code: <#@ template language="C#" hostspecific="True" debug="True" #> <#@ output…
Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
14
votes
4 answers

Log to output window when using T4

I'm using T4 for code generation. Its working well but one thing I'd like to do is some simple logging, I'm happy if this simply took the form of throwing messages into the Output window. Does anyone know a way of doing this? Alternatively, I know…
jamiet
  • 10,501
  • 14
  • 80
  • 159
14
votes
3 answers

Spark T4 templates for ASP.NET MVC

I was just curious if any Spark T4 templates already exist that match/are similar to the out of the box web forms view templates (create, edit, details, etc...). My Google skills didn't lead me to any results.
Chris
  • 4,393
  • 1
  • 27
  • 33
14
votes
2 answers

What does hostspecific signify in a t4 template?

<#@ template debug="false" hostspecific="false" language="C#" #> What does hostspecific attribute signify here.
Vaibhav Jain
  • 33,887
  • 46
  • 110
  • 163
13
votes
3 answers

Text Transformations & Finding Entity Plural (Collection)

If I have a .tt template, I can use entity.Name to write out the name of an entity, e.g: foreach (EntityType entity in ItemCollection.GetItems().OrderBy(e => e.Name)) {; WriteLine(entity.Name); } Using normal transformations or T4,…
Richard
  • 5,810
  • 6
  • 29
  • 36
13
votes
7 answers

Are there any alternatives to T4 templates and EnvDTE for cross platform asp.net 5 development?

We currently use T4 templates to generate C# code files based on C# Code (Entity POCO's) and the EDMX (in older applications) Moving to ASP.NET 5 with a view to support cross platform development, are there any code generation tools available that…
Mark Redman
  • 24,079
  • 20
  • 92
  • 147