Introduction
At the time of adding new project item file, the item template will generate coding for the text file, whether it is a source code or XML generation. In Visual Studio, the code generation is done using T4 text templates which is a mixture of text blocks and control logic to generate a text file.
Background
Part I and part II of this article can be viewed using the links below:
T4 Text Template Generators
Text file generation can be done using T4 text templates in two ways. We can run the T4 text templates during runtime or design-time.
Runtime T4 text templates do not require Visual Studio. It is mostly used to generate HTML or XML report files. Design-time T4 text templates can be used to generate source code files in Visual Studio. It is mostly used to read an input data file or database and generate a source code of either .cs or .vb.
We can customize the text template generation to implement our own control logic.
Here, we will see how to customize design-time T4 text template generators by generating .aspx page file.
Creation of Text Template Generators
- Create a new Web Application
- Add a new Item dialog box and select Text Template from the Templates pane. It will create an extension of .tt.
The ViewPage
text template displays data from the custom data class in a table element. Whenever you make changes in the Text Template and click save, it will ask for running the Text Template generator. A new file ViewPage.aspx will be generated.
Add the following code in the ViewPage.tt Text Template. CustomTool
property of the Text Template file will be set to TextTemplatingFileGenerator
.
The '<#@ template
' directive in the first line of ViewPage.tt file specifies that it is a template file. The output directive specifies the extension name of the generated code file. Here, it will be ViewPage.aspx. The assembly directive is used for referencing other assemblies. The import
directive is the same as using in C#.
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".aspx" #>
<#@ assembly name="$(SolutionDir)bin\WebApplication1.dll" #>
<#@ assembly name="System.Data.Entity" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="WebApplication1" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Collections.ObjectModel" #>
<%@ Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<body>
<h2>Customer Lists<h2>
<#
data cls = new data();
ObservableCollection<Customer> customers = cls.GetCustomers();
#>
<table border="1">
<tr>
<td>Id</td> <td>Name</td>
</tr>
<# foreach (Customer c in customers)
{
#>
<tr>
<td> <#= c.CustId #> </td>
<td> <#= c.CustName #> </td>
</tr>
<#
}
#>
</table>
</body>
</html>
Here is the code for creating a sample data class:
public class data
{
public data()
{ }
public System.Collections.ObjectModel.ObservableCollection<customer> GetCustomers()
{
System.Collections.ObjectModel.ObservableCollection<customer> customers =
new System.Collections.ObjectModel.ObservableCollection<customer>();
customers.Add(new Customer(1, "John"));
customers.Add(new Customer(2, "Steve"));
customers.Add(new Customer(3, "Raj"));
customers.Add(new Customer(4, "Jagan"));
customers.Add(new Customer(5, "George"));
return customers;
}
}
public partial class Customer
{
public Customer()
{ }
public Customer(int custid, string custname)
{
this.CustId = custid;
this.CustName = custname;
}
private int custId;
private string custName;
public int CustId
{
get { return custId; }
set { custId = value; }
}
public string CustName
{
get { return custName; }
set { custName = value; }
}
}
public partial class Customers :
System.Collections.ObjectModel.ObservableCollection<customer>
{
}
Running Text Templates
When you run save ViewPage.tt text template, it will ask whether to run custom tool.
To run Text Template files, any of the following can be done:
- Save the ViewPage.tt text template file.
- Right-Click the ViewPage.tt text template, and Click Run Custom Tool.
- Build the Solution. It will run all the text templates defined in the solution.
Conclusion
Likewise, we can generate text files by implementing business logic using T4 Text Templates. It is more powerful when it comes to generating database layer business logic. We can extract information from data model as XML, and implement complex business logic for data layers.