How to create a folder using the .tt template?

I am trying to create a template that will create a series of files in different folders, but I have not found any samples.

+5
source share
1 answer

You can use RenderToFilefrom t4Toolbox to do this.

Excerpt from sample documentation from 2016.10.12:

  • Create a Visual Studio solution with two C # class library projects ClassLibrary1.csproj and ClassLibrary2.csproj.

  • Add a new code generation file called CodeGenerator.tt to the first class library project.

  • Change the contents of the new file so that it looks like this

<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#
    SampleTemplate template = new SampleTemplate();
    template.Output.File = @"SubFolder\SampleOutput.txt";
    template.Output.Project = @"..\ClassLibrary2\ClassLibrary2.csproj";
    template.Render();
#>
<#+
    public class SampleTemplate : Template
    {
        public override string TransformText()
        {
            this.WriteLine("Hello, World!");
            return this.GenerationEnvironment.ToString();
        }
    }
#>

Original Documentation

Wayback machine

+4
source

All Articles