

For example, the following shows how to print some text in an expression block as part of a standard control block. You must always terminate a given control block before you open another one. Control blocks may not be nested inside each other. It is also a good idea to initialize your local variables to sensible values when you declare them, particularly when you are including multiple text templates. One way to ensure this is to add a string to each local variable name identifying the text template in which it was declared. If you are including other text templates, you must make sure that variable names are unique across all included templates.
#Text blocks code#
Since all the code in the standard and expression control blocks in a text template is generated as a single method, you should make certain that there are no conflicts with the names of local variables. You can use only one language in a given text template, including all the text templates you include with the include directive.

You can generate C# by using Visual Basic code or vice versa. The language you use in control blocks has nothing to do with the language or format of the text you generate in a text template. (For more information about the template directive, see T4 Text Template Directives.) The default language is C#, but you can specify Visual Basic with the language parameter of the template directive. You can use either C# or Visual Basic code in a text template.

You should keep in mind the following considerations when you use control blocks: (For more information about including other text templates with the include directive, see T4 Text Template Directives.) You could call this function from a standard block or from another class feature block: Īll the code in all the standard and expression control blocks in a single template (including all the code in included templates) is combined to form the TransformText() method of the generated code. You can create a function that generates output by embedding text and expression blocks inside a class feature control block. Each included file can have standard blocks followed by class feature blocks. However, this restriction does not apply to the result of using directives. For example, the following class feature block capitalizes the first letter of the attribute name (or, if the name contains whitespace, it capitalizes the first letter of every word): Ī class feature control block must not be followed by standard control blocks in the same template file. The most common use of class feature blocks is to provide helper functions for code in other parts of the text template. You can use class feature control blocks to add methods, properties, fields, or even nested classes to your text template. For example, with the example above, you can print the names of the attributes to the output file by modifying the code block as follows: For example, this fragment generates an output line in each loop iteration: Īlways use, as follows:Įxpression control blocks are used for code that provides strings to be written to the output file. You can embed plain text inside a compound statement such as if or for. XmlDocument xDoc = new attributes = xDoc.Attributes įoreach (XmlAttribute attr in attributes) For example, the following standard block gets the names of all the attributes in the XML document: Standard control blocks contain statements. can contain methods, fields and properties. There are three kinds of control blocks, which are distinguished by their opening brackets:
#Text blocks for mac#
Applies to: Visual Studio Visual Studio for Mac Visual Studio CodeĬontrol blocks let you write code in your text template in order to vary the output.
