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#
In Visual Studio, on the File menu, point to New, and then click Project.
The New Project dialog box appears.
Under Projects, select the Visual C# node.
Under Visual Studio installed templates, select Class Library.
In the Name box, type a name.
For example, type CustomHost.
Check the Create directory for solution check box.
Click OK.
The system creates a new class library project, which opens in Visual Studio.
On the Project menu, click Add Reference, and then click the .NET tab.
In the Component Name column locate Microsoft.VisualStudio.TextTemplating and select it.
Click OK.
In Solution Explorer, double-click Class1 to open it in the editor.
At the top of the code window, after the existing using statements, add the following code:
using Microsoft.VisualStudio.TextTemplating;Locate the following code:
public class Class1 { }Highlight Class1 and type a new name. For example, type Host.
Put your cursor after the new class name and type the following code:
: ITextTemplatingEngineHostIn 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.
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.
On the File menu, click Save All.
On the Build menu, click Build Solution.
Creating a Custom Host in Visual Basic
To create a custom host in Visual Basic
In Visual Studio, on the File menu, point to New, and then click Project.
The New Project dialog box appears.
Under Projects, select the Visual Basic node.
Under Visual Studio installed templates, select Class Library.
In the Name box, type a name. For example, type CustomHost.
Check the Create directory for solution check box.
Click OK.
The system creates a new class library project, which opens in Visual Studio.
On the Project menu, click Add Reference, and then click the .NET tab.
In the Component Name column locate Microsoft.VisualStudio.TextTemplating and select it.
Click OK.
In Solution Explorer, double-click Class1 to open it in the editor.
At the top of the code window, add the following code:
Imports Microsoft.VisualStudio.TextTemplatingLocate the following code:
Public Class Class1 End ClassHighlight Class1 and type a new name. For example, type Host.
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 ITextTemplatingEngineHostPress 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.
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.
On the File menu, click Save All.
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