3

I downloaded and installed the T4Toolbox to work with T4 templates more easily... however, I can't even figure out how to run the Generator or CSharpTemplate.

Here is an example file I created:

Template1.tt:

<#+
    public class Template1 : CSharpTemplate
    {
        public override string TransformText()
        {
            base.TransformText();
#>

Hello world

<#+
            return this.GenerationEnvironment.ToString();
        }
    }
#>

I have compiled my application and saved the file, yet no output file(s) are created. :/

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
michael
  • 14,844
  • 28
  • 89
  • 177

2 Answers2

1

You need to have another Template file which will call this template class "Template1 " to render & generate text.

Lets name that file as BuildTemplate.tt and that should contain below code

  <#@ template language="C#" debug="True" #>
  <#@ include file="T4Toolbox.tt" #>
  <#@ include file="Template1.tt" #>

 <#
 Template1 ta = new Template1 ();
 ta.Render();
 #>

Now just right click on file "BuildTemplate.tt" and select "Run Custom Tool". A .cs file will be generated.

If you want to configure other properties i.e. generate at some specified project, Set CopyToOutputDirectory true/false then you can configure them as below

    ta.Output.File = string.Format("Entity\\Entity.cs");
    ta.Output.PreserveExistingFile = true;
    ta.Output.Project = @"..\<Project Folder Path>\<Project File Name>.csproj"; 

You can find more info on configuring properties at http://www.olegsych.com/2010/03/t4-tutorial-integrating-generated-files-in-visual-studio-projects/

Risky Pathak
  • 598
  • 5
  • 16
0

CSharpTemplate and Generator are classes in the T4Toolbox library. In order to include it to your template, add this line at the top of your .tt file:

<#@ include file="T4Toolbox.tt" #>
checkmate711
  • 3,301
  • 2
  • 35
  • 45