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
31
votes
6 answers

How to output namespace in T4 templates?

I have a T4 template for a class set up with TextTemplatingFileGenerator Custom Tool in Visual Studio: <#@ template language="C#v3.5" hostspecific="True" debug="True" #> <# var className =…
Hallgrim
  • 15,143
  • 10
  • 46
  • 54
30
votes
5 answers

Is there code generation API for TypeScript?

When I needed to generate some C# code, for example DTO classes from xsd schema, or an excel table, I've used some roslyn API's. Is there something simmilar for typescript? [EDIT]: I've end up using ts-morph
Liero
  • 25,216
  • 29
  • 151
  • 297
30
votes
8 answers

How can I resolve this error: Loading the include file 'EF.Utility.CS.ttinclude' returned a null or empty string

How can I resolve these errors: Loading the include file 'EF.Utility.CS.ttinclude' returned a null or empty string. Value does not fall within the expected range. Tools Used: VS 2012, Entity Framework 5.0.0, T4MVCExtensions 3.5.0, .NET Framework…
Arnold
  • 515
  • 1
  • 9
  • 18
28
votes
9 answers

For what purposes have YOU used T4?

T4 has existed for several years in Visual Studio, but doesn't get a lot of attention. However, for those that know it, there seems to be some very creative and useful purposes. I am researching some different ways that T4 is used, and I would…
Chris Melinn
  • 2,046
  • 1
  • 17
  • 18
27
votes
3 answers

Determine solution configuration (debug/release) when running a T4 template

I have a T4 template that can output either optimized content or standard content based on a flag. Currently I'm manually changing the flag based on my needs. What I'd love to do is set the flag based on the Configuration of the Solution in Visual…
Emil Lerch
  • 4,416
  • 5
  • 33
  • 43
27
votes
1 answer

T4 Template is Generating Extra New Lines on Some PCs

While using T4 classes for entity framework there are a couple of developers who generate classes with one extra new line for every line generated. I'm wondering if this is some kind of setting that needs to be changed so that their T4 generated…
Thomas927
  • 853
  • 1
  • 11
  • 19
26
votes
3 answers

NHaml T4 templates for CRUD?

I want to ask if anyone has or has seen T4 templates for NHaml that are the same as the default T4 CRUD (List, Create etc) templates from MVC 2.0?
Łukasz W.
  • 9,538
  • 5
  • 38
  • 63
25
votes
4 answers

T4 Generation: Where does VsNamespaceSuggestion() pull from?

Does anybody know, in a .tt file, where code.VsNamespaceSuggestion() gets its namespace from? I'm having an issue where I had to change a solution's namespace, and I swear I've changed it everywhere (folder names, filenames, project names, project…
James in Indy
  • 2,804
  • 2
  • 25
  • 25
25
votes
3 answers

T4 Get Current Working Directory of Solution

I am using T4 in Visual Studio 2010, and I want to iterate over the files in my solution, however I have found that T4 source generation works in a kind of a sandbox, and the current working directory is inside of the Visual Studio 10 directory in…
aceinthehole
  • 5,122
  • 11
  • 38
  • 54
24
votes
6 answers

T4 templates - avoid empty lines from included files

I am splitting T4 code in separate files for modularity and reuse but I am finding out that each file costs me an empty line in output. For example: <#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".ttinclude"…
Tony
  • 1,566
  • 2
  • 15
  • 27
23
votes
2 answers

How to disable #line directives being written to the T4 generation output file

I have encountered a small problem with my T4 code generation. I have broken my T4 templates up into separate files and placed them in various directories, I have done this so parts of my code generation may be re-used in multiple projects, e.g.…
Lukazoid
  • 19,016
  • 3
  • 62
  • 85
23
votes
2 answers

Generated (by T4) file Build Action gets reset to Build

We have a database project in Visual Studio 2013. In this project we have a .tt file which generates .sql script. The problem is after generation the build action of the generated file is automatically set to Build. If we change it manually to None,…
nightcoder
  • 13,149
  • 16
  • 64
  • 72
23
votes
4 answers

T4 reports Compiling transformation: Invalid token 'this' in class, struct

Attempting to run the T4 templates for Immutable Object Graph gives errors of…
Mitch
  • 21,223
  • 6
  • 63
  • 86
23
votes
7 answers

Creating T4 templates at runtime (build-time)?

We are building an inhouse application which needs to generate HTML files for upload into eBay listings. We are looking to use a template engine to generate the HTML files based on database and static fields that we have pre-defined. The template…
Amberite
  • 1,379
  • 3
  • 14
  • 27
23
votes
8 answers

T4MVC Error - T4 file is blocked or in a non trusted zone

I am running Visual Studio 2010 on a Windows 7 virtual machine inside of Parallels Desktop 8 on a Mac Book Pro. I'm trying to incorporate T4MVC into a new MVC4 project. I installed the T4MVC package from Nuget and immediately started getting this…
jdavis
  • 8,255
  • 14
  • 54
  • 62