Comments in T4 Templates

This seems like such a basic question, but I could not find the MSDN article or the StackOverflow question to which he answers: is it possible to make comments on strings or block comments in T4 templates? I do not want to generate code with comments (this is easy and simple), but rather comments on the blocks of my T4 markup. Is it possible?

+43
comments t4
Aug 23 '11 at 15:04
source share
2 answers

To include comments as part of the control code, they must be inside some code block

<# // Hello this is a comment #> for example 

or

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

Sometimes you need to click the close tag on the next line if you are sensitive to extra newlines on the output.

If you want to comment on entire blocks of markup, unfortunately, there is no direct solution, and the result will be pretty ugly.

You can do this by escaping the tags you want to comment on, for example:

 \<# my control code \#> 

and then putting this inside a comment in another block as follows:

 <# // \<# my control code \#> #> 
+51
Aug 23 '11 at 18:26
source share

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

 <# #if false foreach(var typeName in typeNames) { var className = typeName + "Adapter"; #> // ... <# } #endif #> 
+15
Nov 30 '13 at 0:18
source share



All Articles