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

T4: Is it possible to use a script to fill in a template, while still providing sections via the script?

I have a template that generates a class and a complementary interface to go with it from a script like so: <#@ template language="C#v3.5" hostspecific="True" debug="True" #> <#@ output extension="cs" #> <#@ include file="T4Toolbox.tt" #> <#@…
Maslow
  • 18,464
  • 20
  • 106
  • 193
0
votes
2 answers

I'm having an issue with compliling database objects in SubSonic 3.0 due to project and connection string location issues

i'm having a issue with the T4 templates for generating code. I'm wondering if anyone can help me with an issue I have. I want to store the connection string for use with SubSonic 3.0 in a default location such as the root of the website (Web.Config…
Anthony
  • 441
  • 1
  • 5
  • 20
0
votes
1 answer

T4 Template for Mvc using Reflections

hey i am new to creating T4 Templates for MVC 4. i am stuck with this part of the code. i have a custom class and i want to get the properties of a different class. Eg: class name is WOM and the properties that i want to retrieve is from another…
0
votes
2 answers

Creating a Visual Studio project directory using T4

I am trying to use T4 for source code generation. Mostly, I am able to generate individual files using it. How do I create a complete Visual Studio directory (preferably separate from the T4 template directory) having the below sample…
Ajit Singh
  • 1,016
  • 13
  • 26
0
votes
0 answers

MVC T4 Scaffold templates and annotations

Can you make custom annotations,something like [MyAnnotation] public int Random {get; set;} The reason I'm asking is I'm using T4 templates from Steven Sanderson Scaffolder and I'm trying to use my custom annotations so I can mark which…
Medo
  • 968
  • 1
  • 11
  • 26
0
votes
1 answer

T4 runtime template path specification

I have a quite a bit of T4 runtime templates in my solution and I'd like to organize them in folders, but my builds fail as soon as I do so. The failures are simply because of template can't be found. How can import or add a search path for t4…
0
votes
3 answers

Generating code from a template using different parameters, during every build

I'm writing a somewhat complicated library in C#, and I need to generate a lot of boilerplate code automatically, from a template. Specifically, I want to achieve something like the following: public partial class $ClassName$ { /// ... …
GregRos
  • 8,667
  • 3
  • 37
  • 63
0
votes
1 answer

T4 Template c# understand the difference

I use the following code and I not understand the following : 1. why I don't see the definition string name inside the txt file or the for statement since this is inside the tag 2.if I want to see it do I need to use different tag? <# string…
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88
0
votes
1 answer

T4 template as a part of a library (DLL) that generates classes

I want to write a template that generates classes based on some attributes of some properties on the already exsisting classes. I'm pretty sure T4 templates let me do that, but what I want to do is to have those attributes defined in my class…
Luka Horvat
  • 4,283
  • 3
  • 30
  • 48
0
votes
2 answers

TFService MSBUILD and T4 Code Generation

We are using a T4 file to manage our assembly versioning; Basically we are following these procedure to regenerate our Assembly versionings on each build. This is done by having the .csproj use the Modeling SDK for VS 2013 in order to customize T4…
TheFastCat
  • 3,134
  • 4
  • 22
  • 32
0
votes
1 answer

Subsonic adding a DB provider for Simple Repository

I was wondering--what part of Subsonic 3 needs to be modified to support a different database under a Simple Repository scenario? I haven't found any documentation on this point. Subsonic 3 seems to be all about the T4 templates but when I check…
Alex
  • 322
  • 2
  • 13
0
votes
1 answer

Entity Framework with Poco and navigation properties

I am using Poco Generator with entity framework, And Poco Proxy class have been generated successfully, For a single table all the things goes fine, herein I've listed the main part of my classes and explain relations between tables. There are 3…
Aria
  • 3,724
  • 1
  • 20
  • 51
0
votes
1 answer

T4 -> C# class to Knockout View Model in Typescript

I'm looking for some sort of T4 or similar way to take C# classes and generate both the interfaces for the data that will come across the wire AND the view models in a strongly typed fashion. I found typelite which works well for interfaces, but I'm…
James Hancock
  • 3,348
  • 5
  • 34
  • 59
0
votes
1 answer

Poor performance for POCO Generator

I am working on a Legacy application, and we have a poor performance with Entity Framework (4.0.0) and massive insert. When I tried the POCO Generator (T4), the issue was worse, the SaveChanges was three times longer. This is huge, if you have any…
rad
  • 1,857
  • 1
  • 20
  • 30
0
votes
2 answers

Could not find Microsoft.VisualStudio.TextTemplating assemblies

When I invoke the Add Reference dialog, I got this: I have both Visual Studio 2010 and 2012 installed on the computer. What's wrong?
nim
  • 384
  • 2
  • 14