How to: Add a Statement to Text Templates

You can use text templates to generate code, XML, reports, or other artifacts from models. You can use Statement Syntax blocks to add Visual Basic or Visual C# code to your template. The code should be expressed using the language that is declared in the language parameter of the template directive.

To add a statement to a text template

  1. In Solution Explorer, right-click the text template file that you want to edit, and then click Open.

    The template opens in the editor.

  2. In text templates, use an opening "<#" tag and a closing "#>" tag to delineate code statements. Add statement tags to your text template with code, as shown in the following example.

    In this example, the code loops through each ModelType in ClassModel.Types:

    <#
       foreach(ModelType type in this.ClassModel.Types)
       {
    #>
            // templateblocks
    
    <#
       }
    #>
    
    <#
       Dim type as ModelType
       For Each type in Me.ClassModel.Types
    #>
            // template blocks
    
    <#
       Next
    #>
    

For an example of how to create a fully coded and running 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

Adding Code to Text Templates

Using Built-in Directives in Text Templates

Generating Artifacts Using Text Templates