How to: Add an Expression to Text Templates

You can use text templates to generate code, XML, reports, or other artifacts from models. 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 that a model holds to produce an output file.

The Expression Syntax (Domain-Specific Languages) block contains strings that are generated in the culture specified in the culture parameter of the template directive. Use the Expression Syntax (Domain-Specific Languages) block to add an expression. For more information, see How to: Specify a Culture in Text Templates.

To add an expression 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. In text templates, use an opening "<#=" tag and a closing "#>" tag toi delineate code expressions.

  2. Add the following expression to the template:

    <#
       foreach(ModelType type in this.ClassModel.Types)
       {
          if (type is ModelClass)
          {
    #>
             <#= type.Name #>
    <#
          }
       }
    #>
    
    <#
       Dim type as ModelType
       For Each type in Me.ClassModel.Types
          If TypeOf(type) Is ModelClass Then
    #>
             <#= type.Name #>
    <#
          End If
       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