Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comments in T4 Templates

Tags:

comments

t4

This seems like such a basic question, but I haven't been able to find an MSDN article or StackOverflow question that answers it: is it possible to make line comments or block comments in T4 templates? I'm not looking to generate code with comments (that's easy and straightforward) but rather comment out blocks of my T4 markup. Is that possible?

like image 817
amoss Avatar asked Aug 23 '11 15:08

amoss


People also ask

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. Typically, you write the templates so that they vary the code that they generate according to data from a model. A model is a file or database that contains key information about your application's requirements.

What are 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 transform all T4 templates?

t4 is basically a tool built into VS for doing text transformation, typically for doing code generation. Transform All T4 Templates searches your solution for *. tt files and executes them to create other text, again typically source code, files.


2 Answers

To include comments as part of control code, they need to be inside a code block of some sort, for example

<# // Hello this is a comment #>

or

<#+ // Hello this is a comment in a class feature block #>

Sometimes you need to push the close tag to the next line if you're sensitive to extra newlines in the output.

If you want to comment out whole blocks of markup, there isn't a straightforward solution unfortunately, and the result gets rather ugly.

You can do it by escaping the tags that you'd like to comment, like so:

\<# my control code \#>

and then placing that inside a comment in another block like so:

<# // \<# my control code \#> #>
like image 152
GarethJ Avatar answered Oct 19 '22 05:10

GarethJ


The best way to add block comment is to use #if and #endif

<#
   #if false
   foreach(var typeName in typeNames)
   { 
       var className = typeName + "Adapter";
#>
    // ...
<#  
    }
    #endif
#>
like image 22
engineforce Avatar answered Oct 19 '22 05:10

engineforce