Comments in T4 Templates

amoss picture amoss · Aug 23, 2011 · Viewed 14.1k times · Source

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?

Answer

GarethJ picture GarethJ · Aug 23, 2011

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 \#> #>