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
184
votes
23 answers

Get Visual Studio to run a T4 Template on every build

How do I get a T4 template to generate its output on every build? As it is now, it only regenerates it when I make a change to the template. I have found other questions similar to this: T4 transformation and build order in Visual Studio…
JoelFan
  • 37,465
  • 35
  • 132
  • 205
69
votes
1 answer

How can I use Linq in a T4 template?

I am using T4 to generate some screens and middle-tier code for a project, and would like to use Linq to simplify some of my template code. However, when I try to use Linq, the template reports a syntax error.
GalacticCowboy
  • 11,663
  • 2
  • 41
  • 66
59
votes
4 answers

Improve navigation property names when reverse engineering a database

I'm using Entity Framework 5 with Visual Studio with Entity Framework Power Tools Beta 2 to reverse engineer moderately sized databases (~100 tables). Unfortunately, the navigation properties do not have meaningful names. For example, if there are…
marapet
  • 54,856
  • 12
  • 170
  • 184
58
votes
2 answers

Comments in T4 Templates

This seems like such a basic question, but I haven't been able to find an MSDN article or StackOverflow question that answers it: is it possible to make line comments or block comments in T4 templates? I'm not looking to generate code with comments…
amoss
  • 1,571
  • 1
  • 15
  • 27
53
votes
7 answers

how can I get intellisense in a T4 template?

When trying out these tutorials (T4 Tutorial: Creating reusable code generation templates) I noticed that although I was using plain c# I didn't get any intellisense, probably because of the file extension (.tt) Isn't there anyway to say to Visual…
Vitor Silva
  • 17,114
  • 8
  • 33
  • 27
51
votes
3 answers

Why does my T4 template append a number to the file name?

Why do my T4 templates sometimes append a number to the output file and sometimes not? For instance, in one case I might have a template file called Foo.tt and I'll get an output file of Foo.cs. In other cases, I'll get an output file of Foo1.cs. In…
Gregory Higley
  • 15,923
  • 9
  • 67
  • 96
50
votes
4 answers

T4 template adding assembly of existing project in solution

Hi I need to add the assembly of an an existing project in my solution in my T4 Template file. The problem is that my T4 template is in a project called Project.WebApi and the class that I need in my T4 template is inside a project called…
aleczandru
  • 5,319
  • 15
  • 62
  • 112
49
votes
2 answers

Get Project or Relative Directory with T4

How can I get a reference to the directory of the visual studio project or solution or the directory of the t4 template from within a t4 template? I have a template that concatenates a number of files together which are located relative to the…
Blake Taylor
  • 9,217
  • 5
  • 38
  • 41
44
votes
8 answers

Syntax Highlighting for .tt files?

Is there any way to tell Visual Studio 2010 to enable syntax highlighting for .tt files? I do have a directive to set the extension: <#@ output extension=".ascx" #> So I'd like Visual Studio to treat the file as an ascx file for the purpose of…
Michael Stum
  • 177,530
  • 117
  • 400
  • 535
43
votes
1 answer

T4 alternative in .NET Core?

a T4 text template is a mixture of text blocks and control logic that can generate a text file. T4 templating is not natively supported in .Net Core. Can anyone suggest to me T4 alternative in .NET Core? How can we do code generation?
NBM
  • 1,231
  • 4
  • 14
  • 18
39
votes
1 answer

Is there any way to have functions in basic T4 templates?

By basic T4 template, I mean not using T4 Toolkit or any of the add-ins. My T4 is getting a little complicated, but I'd like to keep in self-contained for now. Is there a way have functions in your T4 template, without referencing external…
Patrick Karcher
  • 22,995
  • 5
  • 52
  • 66
37
votes
6 answers

T4 without Visual Studio?

I'm trying to wireup some code gen templates to my team's automated build process. Our SCM team doesn't want Visual Studio on our build machine (which I have a hard time arguing with). Is there a way to install the T4 engine without Visual Studio?
kellyb
  • 1,391
  • 1
  • 11
  • 18
34
votes
6 answers

Cannot find Microsoft.VisualStudio.TextTemplating assembly

I am doing some work with T4 (Text Template Transformation Toolkit) and am trying to get to a point where I can create my own custom text template host. However, all of that relies on the Microsoft.VisualStudio.TextTemplating assembly and I cannot…
Jeffrey Harrington
  • 1,797
  • 1
  • 15
  • 24
32
votes
4 answers

Can't reference an assembly in a T4 template

I have the following code in a tester class in my main assembly, PocoGenerator. This assembly is supposed to use a T4 template to generate POCO's based on L2S entities in a referenced assembly (a project reference), DataObjects. var assemblyName =…
ProfK
  • 49,207
  • 121
  • 399
  • 775
32
votes
3 answers

How not to output default T4 generated file?

I am using T4toolbox to generate a bunch of files, let's say my t4 file name is x.t4, but default it generate a x.txt, which has nothing inside, can I tell t4 engine not to do this?
Fred Yang
  • 2,521
  • 3
  • 21
  • 29
1
2 3
99 100