Tuesday, June 20, 2017

very simple T4 Example / sehr einfaches T4 Beispiel

1) Add Text Template

Add a new item to your project, choose General/Text Template => TextTemplate1.tt will be added to your project

2) copy and paste below to the tt file:


<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".txt" #>

----------------- BEGINN -----------------------



<#
string name ="mike";
#>

Hello <#= name #> !

<# for (int i=0; i<10 i="" p=""> {
WriteLine("test"+i.ToString());
}
#>

------------------ ENDE ! -------------------


3) look at the results


open child file of texttemplate1.tt, should be TextTemplate1.txt should look like this:
----------------- BEGINN -----------------------




Hello mike !

test0
test1
test2
test3
test4
test5
test6
test7
test8
test9

------------------ ENDE ! -------------------

4) Basic Elements of a tt file:

template language: language in which the tempolate is written
assembly: used to execute
import = using
output= output file extension

every normal Text is simply copied to output (like --- BEGINN ...)

<# // is a code block #>

print variable:

No comments: