顯示具有 T4 標籤的文章。 顯示所有文章
顯示具有 T4 標籤的文章。 顯示所有文章

2013年6月13日 星期四

T4 Template Formatting & Utilities

Formatting

image

Example 1:

image

image

Example 2:

image

image

Example 3:

image

image

 

Output Utility

1. Write(string)

2. WriteLine(string)

image

1. Write(string) example:

image

image

2. WriteLine(string) example:

image

image

 

Indentation Utility

1. PushIndent(string)

2. PopIndent()

3.ClearIndent()

4. CurrentIndent

image

1. PushIndent example:

image

image

2. PopIndent example:

image

image

3. ClearIndent example:

image

image

4. CurrentIndent example:

image

image

 

Notification Utility

1. Warning(string)

2. Error(string)

image

Example 1:

image

image

Example 2:

image

image

image

Example 3:

image

image

image

 

Demo Example:

The example to read ConnectionStrings from config file (web.config or app.config)

.tt file

image

image

image

image

image

image

image

App.config file

image

.cs file

image

Program.cs

image

Output result:

image

T4 Template Directives (Assembly, Import, Include)

Three other different Directives can be used in T4 Template

1. Assembly

2. Import

3. Include

 

1. Assembly

image_thumb[20]

image_thumb[25]

Error if no Assembly directive

image_thumb[24]

 

2. Import

image_thumb[26]

image_thumb[30]

image_thumb[28]

 

3. Include

image_thumb[33]

incl1.tt

image_thumb[38]

incl2.tt

image_thumb[34]

Main.tt

image_thumb[36]

Output Result

image_thumb[37]

Add code in incl1.tt

image_thumb[39]

Output Result

image_thumb[40]