<#+ メンバ定義 #>
<#+ と #>で囲んだ部分はフィールドやメソッドの定義となり、<#~#>や<#=~#>の中から参照(利用)できます。
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
using System;
class Hello {
static public void say(string name) {
Console.WriteLine("Hello, {0}",name);
<# foreach ( string fruit in basket ) { #>
<#= fruit #>();
<# } #>
}
<# foreach ( string fruit in basket ) { #>
static private void <#= fruit #>() {
Console.WriteLine("<#= i_like(fruit)#>");
}
<# } #>
}
<#+
string[] basket = { "apple", "banana", "cherry" };
static string i_like(string item) {
return string.Format("I like {0}", item.ToUpper());
}
#>
このテンプレートからは、以下のコードが生成されます。
using System;
class Hello {
static public void say(string name) {
Console.WriteLine("Hello, {0}",name);
apple();
banana();
cherry();
}
static private void apple() {
Console.WriteLine("I like APPLE");
}
static private void banana() {
Console.WriteLine("I like BANANA");
}
static private void cherry() {
Console.WriteLine("I like CHERRY");
}
}
ここで1つご注意。<#+~#>によるメンバ定義は必ずテンプレートの末尾に置かなくてはなりません。ただし、<#+~#>を末尾に置いたテンプレートを<#@ include file="..." #>によって差し込むことは許されています。上記のテンプレートは以下のように書くことができます。
using System;
class Hello {
static public void say(string name) {
Console.WriteLine("Hello, {0}",name);
<# foreach ( string fruit in basket ) { #>
<#= fruit #>();
<# } #>
}
<# foreach ( string fruit in basket ) { #>
static private void <#= fruit #>() {
Console.WriteLine("<#= i_like(fruit)#>");
}
<# } #>
}
<#+
static string i_like(string item) {
return string.Format("I like {0}", item.ToUpper());
}
#>
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ include file="Body.tt.include" #>
<#+
string[] basket = { "apple", "banana", "cherry" };
#>
