Yes, you can. First, define the save procedure in a file with T4 enabled.
SaveOutput.tt
<#@ template language="C#" hostspecific="true" #> <#@ import namespace="System.IO" #> <#+ void SaveOutput(string fileName) { string templateDirectory = Path.GetDirectoryName(Host.TemplateFile); string outputFilePath = Path.Combine(templateDirectory, fileName); File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString()); this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length); } #>
Now every time you call SaveOutput with a file name, it writes the current buffer to this external file. For instance.
Web.tt
<#@ include file="SaveOutput.tt" #> <# GenerateConfig(); SaveOutput("..\..\Web.Config"); #>
If you want to generate multiple files:
<#@ include file="SaveOutput.tt" #> <# GenerateFile1(); SaveOutput("..\..\File1.txt"); GenerateFile2(); SaveOutput("..\..\File2.txt"); #>
source share