How to: Use Escape Sequences in Text Templates

Text template files support using an escape sequence to enable such things as quoted strings. You can also use escape sequences to generate text template tags to output files or to create text templates from text templates. For more information, see How to: Generate Templates from Templates Using Escape Sequences.

注意

Visual Basic does not support escape sequences to enable quoted strings.

You can add escape sequences to text templates using the escape character "\". For example, you can use the following escape characters to retain text template tags in the output file:

\<# … \#>

If you need a text block that includes strings used to delimit other blocks, then you may use the following escape sequences:

  • If either of the two tags has an even number of escape (\) characters preceding it, the following procedure will be applied: suppose there are 2n occurrences of \ before the tag, the 2n occurrences are replaced with n occurrences of \ in the post-parsing code and the tag is considered as a start/end tag.

  • If either of the two tags has an odd number of escape (\) characters preceding, the following procedure will be applied: suppose there are 2n+1 occurrences of \ before the tag, the 2n+1 occurrences are replaced with n occurrences of \ plus the tag itself (<# or #>) in the post-parsing code. The tag is not considered as a start/end tag.

  • In the case of escape (\) appearing anywhere else in any other sequence (apart from escaping " in a directive block), the escape receives no special treatment. For example, \a, \<\#, #\> will be ignored by the parser and will lend no special significance to it. The \ will be left as it is in the post-parsing code.

To use escaping in 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 escape character "\" as needed. For example, to retain the quoted string name, you can use the following syntax:

    <# string name = "\"QuotedStringName\""; #>
    <#= name #>
    

    注意

    For an example of how to use escape sequences to generate a text template from within a text template, see How to: Generate Templates from Templates Using Escape Sequences.

    注意

    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

Text Syntax (Domain-Specific Languages)

Generating Specific Types of Artifacts from Text Templates

Adding Code to Text Templates

Generating Artifacts Using Text Templates

Character Escapes