Visual Studio For Mac T4 Template
Aug 17, 2017 Download Devart T4 Editor for Visual Studio - Edit Visual Studio T4 templates with the help of intellisense, syntax highlighting, easy navigation, multi-level inclusion, outlining, and more.
-->You can generate text strings in your application at run time by using Visual Studio runtime text templates. The computer where the application executes does not have to have Visual Studio. Runtime templates are sometimes called 'preprocessed text templates' because at compile time, the template generates code that is executed at run time.
Each template is a mixture of the text as it will appear in the generated string, and fragments of program code. The program fragments supply values for the variable parts of the string, and also control conditional and repeated parts.
For example, the following template could be used in an application that creates an HTML report.
Notice that the template is an HTML page in which the variable parts have been replaced with program code. You could begin the design of such a page by writing a static prototype of the HTML page. You could then replace the table and other variable parts with program code that generates the content that varies from one occasion to the next.
Using a template in your application makes it is easier to see the final form of the output than you could in, for example, a long series of write statements. Making changes to the form of the output is easier and more reliable.
Creating a Run-Time Text Template in any Application
To create a run-time text template
In Solution Explorer, on the shortcut menu of your project, choose Add > New Item.
In the Add New Item dialog box, select Runtime Text Template. (In Visual Basic look under Common Items > General.)
Type a name for your template file.
Note
The template file name will be used as a class name in the generated code. Therefore, it should not have spaces or punctuation.
Choose Add.
A new file is created that has extension .tt. Its Custom Tool property is set to TextTemplatingFilePreprocessor. It contains the following lines:
Converting an Existing File to a Run-Time Template
A good way to create a template is to convert an existing example of the output. For example, if your application will generate HTML files, you can start by creating a plain HTML file. Make sure that it works correctly and that its appearance is correct. Then include it into your Visual Studio project and convert it to a template.
To convert an existing text file to a run-time template
Include the file into your Visual Studio project. In Solution Explorer, on the shortcut menu of the project, choose Add > Existing Item.
Set the file's Custom Tools property to TextTemplatingFilePreprocessor. In Solution Explorer, on the shortcut menu of the file, choose Properties.
Note
If the property is already set, make sure that it is TextTemplatingFilePreprocessor and not TextTemplatingFileGenerator. This can happen if you include a file that already has the extension .tt.
Change the file name extension to .tt. Although this step is optional, it helps you avoid opening the file in an incorrect editor.
Remove any spaces or punctuation from the main part of the file name. For example 'My Web Page.tt' would be incorrect, but 'MyWebPage.tt' is correct. The file name will be used as a class name in the generated code.
Insert the following line at the beginning of the file. If you are working in a Visual Basic project, replace 'C#' with 'VB'.
<#@ template language='C#' #>
The Content of the Run-Time Template
Template directive
Keep the first line of the template as it was when you created the file:
<#@ template language='C#' #>
The language parameter will depend on the language of your project.
Plain content
Edit the .tt file to contain the text that you want your application to generate. For example:
Embedded program code
You can insert program code between <#
and #>
. For example:
Notice that statements are inserted between <# .. #>
and expressions are inserted between <#= .. #>
. For more information, see Writing a T4 Text Template.
Using the Template
The code built from the template
When you save the .tt file, a subsidiary .cs or .vb file is generated. To see this file in Solution Explorer, expand the .tt file node. In a Visual Basic project, first choose Show All Files in the Solution Explorer toolbar.
Notice that the subsidiary file contains a partial class that contains a method called TransformText()
. You can call this method from your application.
Generating text at run time
In your application code, you can generate the content of your template using a call like this:
To place the generated class in a particular namespace, set the Custom Tool Namespace property of the text template file.
Debugging Runtime Text Templates
Debug and test runtime text templates in the same way as ordinary code.
You can set a breakpoint in a text template. If you start the application in debugging mode from Visual Studio, you can step through the code and evaluate watch expressions in the usual way.
Passing parameters in the constructor
Usually a template must import some data from other parts of the application. To make this easy, the code built by the template is a partial class. You can create another part of the same class in another file in your project. That file can include a constructor with parameters, properties and functions that can be accessed both by the code that is embedded in the template, and by the rest of the application.
For example, you could create a separate file MyWebPageCode.cs:
In your template file MyWebPage.tt, you could write:
To use this template in the application:
Constructor parameters in Visual Basic
In Visual Basic, the separate file MyWebPageCode.vb contains:
The template file could contain:
The template can invoked by passing the parameter in the constructor:
Passing data in template properties
An alternative way of passing data to the template is to add public properties to the template class in a partial class definition. Your application can set the properties before invoking TransformText()
.
You can also add fields to your template class in a partial definition. This enables you to pass data between successive executions of the template.
Use partial classes for code
Many developers prefer to avoid writing large bodies of code in templates. Instead, you can define methods in a partial class that has the same name as the template file. Call those methods from the template. In this way, the template shows more clearly what the target output string will look like. Discussions about the appearance of the result can be separated from the logic of creating the data that it displays.
Assemblies and references
If you want your template code to reference a .NET or other assembly such as System.Xml.dll, add it to your project's References in the usual manner.
If you want to import a namespace in the same way as a using
statement, you can do this with the import
directive:
These directives must be placed at the beginning of the file, immediately after the <#@template
directive.
Shared content
If you have text that is shared between several templates, you can place it in a separate file and include it in each file in which it should appear:
The included content can contain any mixture of program code and plain text, and it can contain other include directives and other directives.
The include directive can be used anywhere within the text of a template file or an included file.
Inheritance between Run-Time Text Templates
You can share content between run-time templates by writing a base class template, which can be abstract. Use the inherits
parameter of the <@#template#>
directive to reference another runtime template class.
Inheritance pattern: Fragments in Base Methods
In the pattern used in the example that follows, notice the following points:
The base class
SharedFragments
defines methods within class feature blocks<#+ .. #>
.The base class contains no free text. Instead, all of its text blocks occur inside the class feature methods.
The derived class invokes the methods defined in
SharedFragments
.The application calls the
TextTransform()
method of the derived class, but does not transform the base classSharedFragments
.Both the base and derived classes are runtime text templates; that is, the Custom Tool property is set to TextTemplatingFilePreprocessor.
SharedFragments.tt:
MyTextTemplate1.tt:
MyProgram.cs:
The resulting output:
Inheritance Pattern: Text in Base Body
In this alternative approach to using template inheritance, the bulk of the text is defined in the base template. The derived templates provide data and text fragments that fit into the base content.
AbstractBaseTemplate1.tt:
DerivedTemplate1.tt:
Application code:
Resulting output:
Related Topics
Design-time templates: If you want to use a template to generate code that becomes part of your application, see Design-Time Code Generation by using T4 Text Templates.
Run-time templates can be used in any application where the templates and their content are determined at compile time. But if you want to write a Visual Studio extension that generates text from templates that change at run time, see Invoking Text Transformation in a VS Extension.
See also
-->In Visual Studio, a T4 text template is a mixture of text blocks and control logic that can generate a text file. The control logic is written as fragments of program code in Visual C# or Visual Basic. In Visual Studio 2015 Update 2 and later, you can use C# version 6.0 features in T4 templates directives. The generated file can be text of any kind, such as a web page, or a resource file, or program source code in any language.
There are two kinds of T4 text templates: run time and design time.
Run time T4 text templates
Also known as 'preprocessed' templates, run time templates are executed in your application to produce text strings, typically as part of its output. For example, you could create a template to define an HTML page:
Notice that the template resembles the generated output. The similarity of the template to the resulting output helps you avoid mistakes when you want to change it.
In addition, the template contains fragments of program code. You can use these fragments to repeat sections of text, to make conditional sections, and to show data from your application.
To generate the output, your application calls a function that is generated by the template. For example:
Your application can run on a computer that does not have Visual Studio installed.
To create a run-time template, add a Preprocessed text template file to your project. Is there any fuzzy ahp and topsis software for mac pro. Alternatively, you can add a plain text file and set its Custom Tool property to TextTemplatingFilePreprocessor.
For more information, see Run-Time Text Generation with T4 Text Templates. For more information about the syntax of templates, see Writing a T4 Text Template.
Design time T4 text templates
Design time templates define part of the source code and other resources of your application. Typically you use several templates that read the data in a single input file or database, and generate some of your .cs, .vb, or other source files. Each template generates one file. They are executed within Visual Studio or MSBuild.
For example, your input data could be an XML file of configuration data. Whenever you edit the XML file during development, the text templates regenerate part of the application code. One of the templates could resemble the following example:
Depending on the values in the XML file, the generated .cs file would resemble the following:
As another example, the input could be a diagram of workflow in a business activity. When the users change their business workflow, or when you start work with new users who have a different workflow, it is easy to regenerate the code to fit the new model.
Design-time templates make it quicker and more reliable to change the configuration when the requirements change. Typically the input is defined in terms of business requirements, as in the workflow example. This makes it easier to discuss the changes with your users. Design-time templates are therefore a useful tool in an agile development process.
To create a design-time template, add a Text Template file to your project. Alternatively, you can add a plain text file and set its Custom Tool property to TextTemplatingFileGenerator.
For more information, see Design-Time Code Generation by using T4 Text Templates. For more information about the syntax of templates, see Writing a T4 Text Template.
Note
The term model is sometimes used to describe data read by one or more templates. The model can be in any format, in any kind of file or database. It does not have to be a UML model or a Domain-Specific Language model. 'Model' just indicates that the data can be defined in terms of the business concepts, rather than resembling the code.
The text template transformation feature is named T4.