Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T4 Template Blank Line or Line Break

Tags:

templates

t4

Is there a way to instruct a T4 template to generate a blank line or line break?

I realize that I can output whitespace in a block which already contains some text. But what about the case when I don't have text to output? I'd like to simply output a blank line. For example, between method calls.

@TobiMcNamobi

I was trying all manner of the usage of the '#>' and '<#+' tags between the method calls below. I seem to have stumbled upon a technique which works ('#> <#+'), but I don't understand why it works. For all I can tell, I'm instructing the template to output a Space.

<#+
public class Blah
{
    /// <summary>
    /// Generates properties.
    /// </summary>
    /// <param name="codeInterface">The interface containing the properties collection.</param>
    public void GenerateProperties(string blah)
        {
            IEnumerable<String> properties = codeInterface.Properties;

            foreach (var property in properties)
            {
                if (!codeInterface.IsInterface)
                    {
                        this.GeneratePrivateVariable(property);
                    }
            }#> <#+

            foreach (var property in properties)
            {
                if (codeInterface.IsInterface)
                {
                    this.GenerateInterfaceProperty(property);
                }
                else
                {
                    this.GenerateClassProperty(property, codeInterface as string);
                }
            }
        }
#>

Edit: It seemed to work at first. I generated as per the above and it generated the output I expected. Now it does not. This does:

                    }
            }#> (<-- a single space here, after the tag)
<#+

            foreach (var property in properties)

It's rather difficult to convey visually. In essence:

  1. I type the closing tag (#>)
  2. I type a Space
  3. I hit Enter
  4. I type the opening tag (<#+)

I don't know if this is the correct way, but it seems to work.

like image 982
whistle britches Avatar asked Nov 01 '22 03:11

whistle britches


1 Answers

You example writes one space on the "empty" line. To fix it you can use Write("\r\n");

In your example:

foreach (var property in properties)
{
    if (!codeInterface.IsInterface)
    {
         this.GeneratePrivateVariable(property);
    }
}
Write("\r\n");

foreach (var property in properties)
{

This should put new line to the generated file.

like image 146
Petr Kramolis Avatar answered Nov 04 '22 11:11

Petr Kramolis