Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual Studio SDK
 How to: Generate Templates from Tem...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Domain-Specific Language Tools 
How to: Generate Templates from Templates Using Escape Sequences 

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

  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 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).

C#
\<#@ 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

NoteNote

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker