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

") Expected" using t4 text templates in c#

Trying to make a conversion .exe between a .gbxml filetype and a .idf filetype by using a t4 text template, i ran into an error. Somehow, this code snippet keeps telling me: Error CS1026: ) expected. This occurs after the 'relative north{deg}");'…
0
votes
1 answer

T4 formatting generated properties

here is my T4 template method to generate property public string Property(EdmProperty edmProperty) { var fieldName =…
Harry89pl
  • 2,415
  • 12
  • 42
  • 63
0
votes
1 answer

Entity Framework, T4 templates, and how to find out if a property has a 1-1 mapping table collapsed

I am using EF5 as my ORM tool. I have a relationship between 3 tables in the DB as follows: Table 1 is the main table. It has a "1 to 0..1" relationship to Table 2. Table 2 has a "many to 1" relationship to Table 3. Now when you import this into…
eyeballpaul
  • 1,725
  • 2
  • 25
  • 39
0
votes
1 answer

Postsharp Resharper Redgate Excessive Logging

I have a C# solution containing Postsharp v3, Resharper 8, Redgate SqlBundle and .Net toolbet + some very large T4 template generated code. When generating code from templates and attempting to open them up a folder on my hard disk…
Jimbo
  • 3
  • 1
0
votes
1 answer

Are there any limitations on what libraries can be imported in a t4 template?

We're trying to learn to use T4 Templates. I have a desire to use the System.Data.Entity.Design.PluralizationServices library in order to better pluralize some Entity Model names within my template, but I've come across some issues in the…
thepaulpage
  • 4,614
  • 2
  • 25
  • 39
0
votes
1 answer

DataTable in T4 template

I'm creating a t4 template that will generate an enum from a number of values in our database. The weird thing is that as soon as I try to create a DataTable in the template code i get the ErrorGeneratingOutput message: <#@ template…
Patrik Hägne
  • 16,751
  • 5
  • 52
  • 60
0
votes
1 answer

Scaffolder unable to load EDMX metadata after upgrading to .NET4.5 and EF5

So I've upgraded an existing MVC4 project to .NET4.5 and EF5. I've run the project and everything works properly. Then I tried to run my custom scaffolder, and it gave me no love. Please forgive me if this is a simple mistake, but our templates…
0
votes
1 answer

Reference dll from another project in T4

I want to be able to reference an assembly in a T4 template but I don't want to bind myself to the debug/release mode version of it. I tried to use the following to include my assembly: <#@ assembly…
Richard Hooper
  • 809
  • 1
  • 12
  • 27
0
votes
1 answer

Replace values in file with object properties

I am going to have template files that I will be using to generate XML files . Templates have server code blocks with object and property for example <%=User.FirstName%> <# include address.tpl pass=User> address.tpl will have…
Kinjal P
  • 19
  • 1
  • 5
0
votes
1 answer

T4 Text Templates Pass object to included Template

I am using Preprocessed Text Templates (.tt) . How can I pass object to another included Preprocessed Text Template? I checked the documentation but didn't find any sample to do that. Thanks
Kinjal P
  • 19
  • 1
  • 5
0
votes
1 answer

Using Microsoft T4 to create data access classes

I'm new to T4 but I have stumbled accross it and it looks very useful. I'm trying to generate some simple DA classes and need some advice one a particular part of my code. How can i generate the following code given that I already have the table…
Ant Swift
  • 20,089
  • 10
  • 38
  • 55
0
votes
0 answers

Code First Reverse Engineering can't change file name

In Entity Framework Power Tools Beta 3 I've used Customize Reverse Engineering Templates to change t4 files in order to rename my Entities classes, but I can't change the file names of those classes using the t4 file templates provided. In the…
0
votes
1 answer

Read class methods properties in mvc4 T4 template

i am working on T4 Templates in web api project in ASP.Net MVC3. i need to get classes and methods from business layer and create cs files using .tt files. also i aneed to get methods only have [PrivateApi] tag. this is my t4 template class. <#…
Krishan
  • 2,356
  • 6
  • 36
  • 47
0
votes
1 answer

T4 PowerShell Array Serialization Error

I am using T4Scaffolding to create a custom scaffold. I use the following PS code to get a list of all domain objects in my project: # List of all domain classes. Get all top level files/folders in the project | drill down to Models folder |…
0
votes
2 answers

Is there a way to use T4 templates with a web site project?

Can T4 templates be used in a web site project? Or more specifically, can Subsonic be used in a web site project? I can run the .tt files in a web application project but it does not nothing on a web site project. I guess they need a…
dtc
  • 10,136
  • 16
  • 78
  • 104
1 2 3
99
100