Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 templates - avoid empty lines from included files

Tags:

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" #> <#@ Import Namespace="System.Collections.Generic" #> <#@ Include file="Includes.tt" #> namespace <#= NameSpace #> { 

If Includes.tt lists 3 other *.tt files, I get 3 blank lines before the namespace. As I am adding code and splitting it in separate *.tt files, this empty space keeps growing. In fact, I packed all the include files into a single Includes.tt, hoping that this will cost me just one empty line. It didn't. I still get one empty line per each file listed in Includes.tt. Is there a way of avoiding this?

Edit: assuming that I am not making just a dumb mistake (and I sincerely hope that I am), the problem is not as trivial as it may appear on the first sight:

a) Reuse and modularity via included T4 files is as old as T4 itself and was mentioned in latest MSDN Magazine article: "Managing Complexity in T4 Code-Generation Solutions".

b) If code is auto-generated, it doesn't mean that it is Ok for it to be badly formatted or poorly legible.

c) With the current solution, in my case, for each and every generated .cs file the reader would have to scroll for one empty page until she starts seeing some generated text. All because I have split my code generation between multiple included .tt files. That just doesn't seem right.

like image 201
Tony Avatar asked May 07 '12 08:05

Tony


People also ask

What is true about T4 templates in Entity Framework?

T4 templates in entity framework are used to generate C# or VB entity classes from EDMX files. Visual Studio 2013 or 2012 provides two templates- EntityObject Generator and DBContext Generator for creating C# or VB entity classes. The additional templates are also available for download.

What is the purpose of using T4 templates?

Design-time T4 text templates let you generate program code and other files in your Visual Studio project.


2 Answers

To add to Tony's answer: You can avoid the very long lines by adding the linebreaks within the T4 brackets like so:

<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".ttinclude" #> <#@ Import Namespace="System.Collections.Generic" #> <#@ Include file="Usings.tt"  #><#@ Include file="PropertyTypeEnum.tt"  #><#@ Include.... #><#@ Include.... #><#@ Include.... #><#@ some other stuff  
like image 194
B3ret Avatar answered Sep 27 '22 20:09

B3ret


Oh well, the solution turned out to be trivial, if somewhat unexpected : just put the include directives next to each other, instead of one below another :

<#@ template debug="false" hostspecific="false" language="C#" #> <#@ output extension=".ttinclude" #> <#@ Import Namespace="System.Collections.Generic" #> <#@ Include file="Usings.tt" #> <#@ Include file="PropertyTypeEnum.tt" #> <#@ Include....  
like image 33
Tony Avatar answered Sep 27 '22 20:09

Tony