How to: Create Custom Text Template Hosts

The text template transformation process takes a text template file as input, and produces a new text file as output. The engine component controls the process. It interacts with a text template transformation host and one or more text template directive processors to complete the process. For more information, see Architecture of the Text Template Transformation Process.

If you want to use the text template transformation functionality from outside Visual Studio, you can write your own custom text template host. To create a custom host, you must create a class that inherits from ITextTemplatingEngineHost. For more information, see Walkthrough: Creating a Custom Text Template Host.

This topic contains steps to complete the following tasks:

  • Creating a Custom Host in C#

  • Creating a Custom Host in Visual Basic

Creating a Custom Host in C#

To create a custom host in C#

  1. In Visual Studio, on the File menu, point to New, and then click Project.

    The New Project dialog box appears.

  2. Under Projects, select the Visual C# node.

  3. Under Visual Studio installed templates, select Class Library.

  4. In the Name box, type a name.

    For example, type CustomHost.

  5. Check the Create directory for solution check box.

  6. Click OK.

    The system creates a new class library project, which opens in Visual Studio.

  7. On the Project menu, click Add Reference, and then click the .NET tab.

  8. In the Component Name column locate Microsoft.VisualStudio.TextTemplating and select it.

  9. Click OK.

  10. In Solution Explorer, double-click Class1 to open it in the editor.

  11. At the top of the code window, after the existing using statements, add the following code:

    using Microsoft.VisualStudio.TextTemplating;
    
  12. Locate the following code:

    public class Class1
    {
    }
    
  13. Highlight Class1 and type a new name. For example, type Host.

  14. Put your cursor after the new class name and type the following code:

     : ITextTemplatingEngineHost
    
  15. In the code window, right-click on ITextTemplatingEngineHost, point to Implement Interface, and then click Implement Interface.

    Visual Studio will fill in the class with the methods from the ITextTemplatingEngineHost class. The methods will be empty. You need to write the code to implement them.

  16. Fill in the code for your custom host.

    For more information, see Walkthrough: Creating a Custom Text Template Host. For the documentation of the individual methods, see ITextTemplatingEngineHost.

  17. On the File menu, click Save All.

  18. On the Build menu, click Build Solution.

Creating a Custom Host in Visual Basic

To create a custom host in Visual Basic

  1. In Visual Studio, on the File menu, point to New, and then click Project.

    The New Project dialog box appears.

  2. Under Projects, select the Visual Basic node.

  3. Under Visual Studio installed templates, select Class Library.

  4. In the Name box, type a name. For example, type CustomHost.

  5. Check the Create directory for solution check box.

  6. Click OK.

    The system creates a new class library project, which opens in Visual Studio.

  7. On the Project menu, click Add Reference, and then click the .NET tab.

  8. In the Component Name column locate Microsoft.VisualStudio.TextTemplating and select it.

  9. Click OK.

  10. In Solution Explorer, double-click Class1 to open it in the editor.

  11. At the top of the code window, add the following code:

    Imports Microsoft.VisualStudio.TextTemplating
    
  12. Locate the following code:

    Public Class Class1
    
    End Class
    
  13. Highlight Class1 and type a new name. For example, type Host.

  14. Put your cursor immediately following the new class name, and press enter to go to the next line. Type the following code, but do not press enter yet:

    Implements ITextTemplatingEngineHost
    
  15. Press enter.

    When you press enter, Visual Studio will fill in the class with the methods from the ITextTemplatingEngineHost class. The methods will be empty. You need to write the code to implement them.

  16. Fill in the code for your custom host.

    For more information, see Walkthrough: Creating a Custom Text Template Host. For the documentation of the individual methods, see ITextTemplatingEngineHost.

  17. On the File menu, click Save All.

  18. On the Build menu, click Build Solution.

Security

For more information, see Security of Text Templates.

See Also

Concepts

Creating Custom Text Template Hosts

Walkthrough: Creating a Custom Text Template Host

Reference

ITextTemplatingEngineHost