This documentation is archived and is not being maintained.

How to: Add Helper Functions to Text Templates 

You create helper functions in text templates by enclosing them in class feature blocks. In text templates, you denote class feature tags by <#+ #>. For more information, see Class Feature Syntax.

In this procedure, you create a text template and then add a simple helper function to it. The helper function returns a string with the spaces removed.

To create a text template

  1. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.

  2. Under Projects, click the Visual Basic or Visual C# node, according to your preference.

  3. Under Visual Studio installed templates, click Class Library.

  4. In Name, type TemplateTest.

  5. Select the Create directory for solution check box, and click OK.

    A class library project is created and opens.

  6. On the Project menu, click Add New Item.

  7. Under Templates, click Text File. In Name, type Test.txt, and then click Add.

  8. In Solution Explorer, click the new text file Test.txt.

    Caution noteCaution

    When you add the text file to the project, it will be highlighted in Solution Explorer but not selected. If you do not click the file, the following step will not work.

  9. On the View menu, click Properties Window.

  10. In the Custom Tool property, type TextTemplatingFileGenerator, and press ENTER.

To add a helper function to a text template

  1. In Solution Explorer, double-click Test.txt to open it in the editor.

  2. Add the following code to Test.txt. These initial directives set the output extension, import a namespace that the function will use, and identify the language (if necessary).

    <#@ output extension=".txt" #>
    <#@ import namespace = "System.Text.RegularExpressions" #>
    

    <#@ output extension=".txt" #>
    <#@ import namespace = "System.Text.RegularExpressions" #>
    <#@ template language="vb" #>
    
  3. Add the following code to Test.txt after the directives. This code is the helper function, and it is wrapped in class feature tags.

    
    <#+
    private string FixWhiteSpaces(string s)
    {
        return(Regex.Replace(s," ","").ToString());
    }
    #>
    

    
    <#+
    Private Function FixWhiteSpaces(ByVal s As String) As String
    
        return Regex.Replace(s, " ", "").ToString()
    End Function
    #>
    
  4. On the File menu, click Save Test.txt.

To use a helper function in a text template

  1. Add the following code to Test.txt. The code should go after the directives and before the class features. This code uses the helper function.

    <#
        WriteLine(FixWhiteSpaces(@"New York"));
        WriteLine(FixWhiteSpaces(@"London"));
        WriteLine(FixWhiteSpaces(@"Seattle"));
        WriteLine(FixWhiteSpaces(@"San Francisco"));
        WriteLine(FixWhiteSpaces(@"New Delhi"));
    #>
    

    <#
        WriteLine(FixWhiteSpaces("New York"))
        WriteLine(FixWhiteSpaces("London"))
        WriteLine(FixWhiteSpaces("Seattle"))
        WriteLine(FixWhiteSpaces("San Francisco"))
        WriteLine(FixWhiteSpaces("New Delhi"))
    #>
    
  2. On the File menu, click Save Test.txt.

  3. In Solution Explorer, right-click Test.txt, and then click Run Custom Tool.

  4. In Solution Explorer, expand Test.txt, and then double-click Test1.txt to open it in the editor.

    The generated text output appears and should look like the following. Notice that spaces have been removed from the names.

    NewYork
    London
    Seattle
    SanFrancisco
    NewDelhi
    

For an example of how to create a fully coded and running text template, see Walkthrough: Creating and Running Text Templates.

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

Show: