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
13
votes
1 answer

How to use T4 with VS2015? (Microsoft.TextTemplating.targets file not found)

I just tried to open one of my projects with Visual Studio 2015 the first time, and it says: The importede project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TextTemplating\Microsoft.TextTemplating.targets" was not found. Confirm…
D.R.
  • 20,268
  • 21
  • 102
  • 205
13
votes
3 answers

How to create multiple output files from a single T4 template using Tangible Editor?

I tried to follow this tutorial: http://t4-editor.tangible-engineering.com/blog/how-to-generate-multiple-output-files-from-a-single-t4-template.html with visual studio 2015 (.Net 4.5) Sample project with error: http://www.filedropper.com/t4fail I…
user2136963
  • 2,526
  • 3
  • 22
  • 41
13
votes
1 answer

ASP.NET MVC 5 Custom Scaffolding Option [t4 templates]

Currently , I am developing a framework , I want to add custom scaffolding option in visual studio menu. By default "MVC5 Controller with views, using Entity Framework" option 3 from the dialog box chooses…
13
votes
1 answer

Using project references as assembly paths in T4

I have a .tt script that needs to reference a couple of external assemblies. Is it possible for the T4 host to automatically include the assemblies referenced in the project - rather than me manually adding an assembly directive for each one? E.g.…
herskinduk
  • 1,187
  • 6
  • 14
13
votes
5 answers

T4 - TT - Using custom classes in TT files

I would like to use my own class define in a CS file in my TT. Example: public class ClassDefinition { public string NameSpace { get; set; } public string Name { get; set; } public string Protection { get; set; } List
Eagle
  • 421
  • 1
  • 3
  • 9
13
votes
2 answers

Control the filename generated by a T4 template?

How can I instruct a T4 template to generate the resulting file with a given file name? I'll try to be more clear. Let's say I have a template called Insert.tt that generates code to insert a record into a table. It has an output extension of "sql",…
Todd Davis
  • 5,855
  • 10
  • 53
  • 89
13
votes
4 answers

ASP.NET MVC custom T4 templates for views

When I'm adding a new strongly-typed view to an ASP.NET MVC project, I can select between different view content templates (Create Details, Edit, List). I have read these are templates based on the Visual Studio T4 code generation engine. How can I…
kay.herzam
  • 3,053
  • 3
  • 26
  • 37
12
votes
3 answers

How do you display the code (not output) that .Net T4 generates?

We have a set of T4 templates we have just migrated forward to VS 2010 and they compile but are no longer working the same. In order to see what is actually going on under the hood it would be useful to see the temporary cs files that T4 generates…
iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
12
votes
1 answer

T4 append output to existing file

Is it possible to make a T4 template output to be merged with an existing file? For example, if a T4 template generates localization resource XML files, is it possible to merge them in some existing resource file?
Sly
  • 15,046
  • 12
  • 60
  • 89
12
votes
3 answers

Automate CRUD creation in a layered architecture under .NET Core

I'm working in a new project under a typical three layer architecture: business, data and client using Angular as a front. In this project we will have a repetitive task that we want to automate: The creation of CRUD. What we want to do is generate…
Pablo
  • 534
  • 11
  • 31
12
votes
4 answers

How to use T4 code generation templates with VS C++ projects?

T4 template files are automatically recognizable by the IDE under C# projects, but I have no clue on how they can be integrated into C++ projects (other than using make files). Any ideas?
Filip Frącz
  • 5,881
  • 11
  • 45
  • 67
12
votes
12 answers

Domain Specific Language resources

I was just listening to some older .Net Rocks! episodes, and I found #329 on DSLs to be interesting. My problem is that I can't find any good online resources for people trying to learn this technology. I get the basics of the creating new…
Eric Haskins
  • 8,505
  • 12
  • 38
  • 47
12
votes
2 answers

Does VS 2010 Express edition support T4 preprocessed templates?

Just installed 2010 Express Edition and I cannot see Preprocess t4 template as an option. Is it not supported in 2010 Express?
epitka
  • 17,275
  • 20
  • 88
  • 141
12
votes
3 answers

How to make connection strings available in a T4 template?

I've written a T4 template where I instantiate an EF context to read some data. The problem is that the context cannot see the connection string from the Web.config. How can I make the connection strings from the Web.config available to the…
Dan
  • 1,555
  • 2
  • 14
  • 30
12
votes
1 answer

Transform T4 Templates

In Visual Studio, what does the menu command "Build" => "Transform All T4 Templates" do?
Jamie
  • 779
  • 1
  • 9
  • 17