T4模板空白行或换行符

whi*_*hes 5 t4 templates

有没有办法指示T4模板生成空白行或换行符?

我意识到我可以在已经包含一些文本的块中输出空格。但是,当我没有文本要输出时该怎么办?我只想输出一个空白行。例如,在方法调用之间。

@TobiMcNamobi

我正在尝试以下方法调用之间使用'#>'和'<#+'标签的所有方式。我似乎偶然发现了一种有效的技术('#> <#+'),但是我不明白为什么它起作用。据我所知,我正在指示模板输出一个空格。

<#+
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);
                }
            }
        }
#>
Run Code Online (Sandbox Code Playgroud)

编辑:起初似乎很有效。我按照上面的方法生成了它,并且生成了我期望的输出。现在没有。这样做:

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

            foreach (var property in properties)
Run Code Online (Sandbox Code Playgroud)

视觉传达是相当困难的。在本质上:

  1. 我输入结束标记(#>)
  2. 我输入一个空格
  3. 我按Enter
  4. 我输入开始标签(<#+)

我不知道这是否正确,但是似乎可行。

Pet*_*lis 2

您的示例在“空”行上写入一个空格。要修复它,您可以使用Write("\r\n");

在你的例子中:

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

foreach (var property in properties)
{
Run Code Online (Sandbox Code Playgroud)

这应该将新行添加到生成的文件中。