3

I have a T4 template called ParentTemplate which include another T4 template called ChildTemplate. The idea is that my ParentTemplate is called and generates a file for each time the ChildTemplate is called. However, my ParentTemplate also creates a file (.cs) which I do not need. The Build Action of the ParentTemplate is set to "None" and the Custom Tool is set to "TextTemplatingFileGenerator". The following code is from the ParentTemplate:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ import namespace="System.IO" #>
<#
    for (int i = 0; i < 3; i++)
    {
#>
<#@ include file="ChildTemplate.t4" #>
<#
    string filename = Path.Combine(path, String.Format("{0}i.txt", myFile));
    File.WriteAllText(filename, this.GenerationEnvironment.ToString());
    this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
    }
#>

In the ChildTemplate is only the text "Hello World". So the output I want is 3 .txt files with the content "Hello World" and that's it. So no other files like ParentTemplate.cs.

Any idea how to prevent the generation of ParentTemplate.cs?

JustRonald
  • 103
  • 3
  • 10

2 Answers2

4

Just clear the Custom Tool property value.

Omid Shariati
  • 1,904
  • 5
  • 22
  • 44
2

From here it says you can do the following to trick Visual Studio into not producing an output file for a .tt file that uses a generator.

<#@ output extension="/" #>

or

<#@ output extension="\\" #>

Note: This is a hack and still produces a warning.
Note: Clearing the Custom Tool property value will not achieve what you want, as that will avoid running the template altogether.

You can also take a much more roundabout (and time-consuming) approach to it:

  • Remove the file from the VSProject during generation with DTEEnv (only available when run within Visual Studio).
  • Use a build task to remove and delete the item after generation (uses MSBuild).
Community
  • 1
  • 1
semaj1919
  • 87
  • 7