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, and 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 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#
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.
A new class library project is created, and 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:
-
Highlight Class1 and type a new name. For example, type Host.
-
Put your cursor after the new class name and type the following code:
: ITextTemplatingEngineHost
-
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, and you must 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.
A new class library project is created, and 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.TextTemplating
-
Locate the following code:
Public Class Class1
End Class
-
Highlight Class1 and type a new name. For example, type Host.
-
Put your cursor after 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
-
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, and you must 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
See Also