How to: Generate Code from Text Templates

You can use text templates to generate code output. For your code output, you need to choose either Visual C# or Visual Basic. To generate code files, you must include valid code blocks. Also, you must ensure that any expressions and write statements generate valid code.

To generate code output from a text template

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

    The file opens in the editor.

  2. Under the directives within your existing code, type your code.

    The following example shows you how to generate code in Visual C#:

    <#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation"#>
    <#@ MyLanguage processor="<YourLanguageName>DirectiveProcessor" requires="fileName='Sample.mmm'"#>
    <#@ output extension=".cs" #>
    
    <#
        foreach(ModelType type in this.ModelRoot.Types)
        {
    #>
        <#= type.Name#>
    <#
        }
    #>
    
  3. On the Solution Explorer toolbar, click Transform All Templates.

    For more information, see How to: Run Text Templates.

Security

For more information, see Security of Text Templates.

See Also

Concepts

Generating Specific Types of Artifacts from Text Templates

Adding Code to Text Templates

Generating Artifacts Using Text Templates

Walkthrough: Creating and Running Text Templates

Domain-Specific Language Tools Glossary