How to: Create Text Templates

A text template file contains a mixture of text blocks and control logic. Use the control logic to combine the text blocks with the data held within a model to produce an output file. There are a variety of uses for text templates. For example, you can use text templates to generate code or documentation from models. A text template contains both text block and control logic that are read along with the model by the text transformation engine. The engine processes the text template to create the appropriate output file that might be source code, xml, html, or some other deployable software artifact.

To create a new text template, follow the procedure below.

To create a new text template

  1. Open a domain-specific language project in Visual Studio.

  2. Press F5 to debug the project in the experimental build.

  3. Click Project, and then click AddNew Item.

    The Add New Item dialog box appears.

  4. In the Templates pane, click Text File.

  5. In the Name text box, enter a name for your new text template, and then click Add.

    注意

    You will find example reporting text templates in the debugging project. These templates have the .reporttemplate extension. You are free to use any extension you want when you create a new text template.

  6. In Solution Explorer, select the text file that you just added.

  7. Click the Categorized icon in the Properties window toolbar and expand the Advanced category.

  8. Under Advanced, set the Custom Tool property to a value of TextTemplatingFileGenerator, and then press ENTER.

    A .cs file is generated and appears in Solution Explorer under the template file that you created earlier.

    You can now add code to the template file. For more information, see Adding Code to Text Templates. For a detailed example of adding functionality to a text template, see Walkthrough: Creating and Running Text Templates.

    注意

    To debug text templates, you must set the debug parameter of the template directive. For more information, see How to: Debug Text Templates.

Security

For more information, see Security of Text Templates.

See Also

Concepts

How to: Run Text Templates

Basic Text Template Tasks

Using Built-in Directives in Text Templates