How to: Generate Templates from Templates Using Escape Sequences

You can create a text template, which in turn creates another text template as its generated text output. To perform this task, you must use escape sequences to delineate the text template tags. If you do not use escape sequences, your generated text template will have a pre-defined meaning. For more information about using escape sequences in text templates, see How to: Use Escape Sequences in Text Templates.

To generate a text template from within 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. Use the backslash (\) as an escape character to produce the necessary markup tags within the text template for directives, statements, expressions, and class features in a separate text template file.

    For more information, see Adding Code to Text Templates.

    \<#@ directive \#>
    \<# statement \#>
    \<#= expression \#>
    \<#+ classfeature \#>
    

Example

The following example uses escape characters to produce a text template from a text template. The output directive sets the destination file type to the text template file type (.tt).

\<#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation"\#>
\<#@ ClassDiagramExample processor="ClassDiagramExampleDirectiveProcessor" requires="fileName='Sample.testcd'" \#>
<#@ output extension=".tt" #>

\<#
   foreach(ModelType type in this.ModelRoot.Types)
   {
\#>
\<#= type.Name \#>
\<#
   }
\#>

The generated text output is a new text template. It contains the following code:

<#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation"#>
<#@ ClassDiagramExample processor="ClassDiagramExampleDirectiveProcessor" requires="fileName='Sample.testcd'" #>

<#
   foreach(ModelType type in this.ModelRoot.Types)
   {
#>
<#= type.Name #>
<#
   }
#>

You can now use the generated template as a template to generate new output. For example, the output might look like this:

Library

Member

Reservation

Book

Item

CD

Title

MultipleAssociation 1

Loan Relation

注意

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

Generating Artifacts Using Text Templates

Using Built-in Directives in Text Templates

Adding Code to Text Templates