You can create a text template that creates another text template as its generated text output. To perform this task, you must use escape sequences to delineate the text template tags, which would otherwise have a pre-defined meaning in the text template. 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
-
In Solution Explorer, right-click the text template file that you want to edit, and then click Open.
The template opens in the editor.
-
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 produces a text template from a text template using escape characters. 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 that gets created is a new text template, which 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
Note |
|---|
| To debug text templates, you must set the debug parameter of the template directive. For more information, see How to: Debug Text Templates. |
Security
See Also