This project is read-only.

Mysterious extra line breaks

Feb 9, 2009 at 4:02 AM
I've been experimenting with T4 and the T4 Toolbox.  My generated code is getting some extra line breaks inserted at the top of the file.  This drives me crazy.  I'm very particular about my line breaks.

In general, I've found that T4 isn't very intuitive about where and when it puts in line breaks.  I end up having to put my <# ... #> code snippets up on the previous line to avoid an extra, unwanted line break.  This reduces template readability.  Any thoughts on how best to handle this issue?
Feb 9, 2009 at 12:54 PM

What kind of hierarchy of templates do you create? Do you keep the one suggested from T4Toolbox?

T4Toolbox suggests that you have a "generator" template that includes calls to the "template" files. You instantiate your generator template, from an other template, usually a "file" template. Using this hierarchy, there is no way to have blank lines in the top of the generated files from your templates.

George J.
Feb 9, 2009 at 6:11 PM
George is right. If you are using Template.RenderToFile method, there should be no extra blank lines in the generated output files. However, if you are using Template.Render, it writes generated content to the main output file. T4 may generate extra blank lines in the beginning of the main output file when generating code from a template that "includes" one or more other templates with multiple processing directives and code blocks. The workaround is indeed to put <# of the next block or directive on the same line with the previous #>. You can find out exactly what is causing the blank lines by setting debug parameter of the template directive to True and examining the intermediate .cs/.vb file produced by T4 in the TEMP directory. Look for the file names and line numbers in the #pragma directives T4 places in the intermediate file.
Feb 9, 2009 at 7:10 PM
Thanks guys.  I'm experience what Oleg is describing.  I do have a template hierarchy.  I am using includes.  The files generated with Template.RenderToFile are okay, but the primary output file has the extra line breaks at the top.

Beyond this problem, though, I still find that T4 is just inconsistent in where it puts line breaks.  Maybe I'm doing something wrong.

So I've come up with perhaps a radical solution ... I've given up on using the templating aspects of T4, and have resorted to simply putting everything in one big <# #> block and using WriteLine()'s to generate everything.

There are pros and cons to this approach.  In some ways, the template is less readable, but in other ways, I find it more readable.  When I'm doing for loops to generate a sequence of code, I find it more readable.  Also, I like using PushIndent() and PopIndent() in combination with WriteLine()'s to be much more explicit about the indenting, and also makes it easier to change the indenting if needed.  Line breaks are much more predictable and explicit with this usage.  An obvious downside is that it is a lot of typing.  I end up typing "WriteLine" a lot.

I ran into a similar problem while working on my XmlGen# project (see, which generates code directly from C# without using T4.  I ended up creating methods called "W" and "WL" to shorten typing and improve overall readability of the code generation sections.  Oh, and speaking of using XML data binding generation in combination with code generation, I tried using T4 Toolbox's XsdProcessor but I found it to be very finicky and it gave me lots of strange errors that I couldn't figure out.