T4 Include 지시문

Visual Studio에서 <#@include#> 지시문을 사용하여 다른 파일의 텍스트를 텍스트 템플릿에 포함할 수 있습니다.텍스트 템플릿에서 첫 번째 클래스 기능 블록 <#+ ... #> 앞의 어느 곳에나include 지시문을 둘 수 있습니다.포함된 파일에는 include 지시문 및 다른 지시문도 포함할 수 있습니다.따라서 템플릿 간에 템플릿 코드와 상용구 텍스트를 공유할 수 있습니다.

include 지시문 사용

<#@ include file="filePath" #>
  • filePath는 현재 템플릿 파일에 대해 상대적인 경로이거나 절대 경로일 수 있습니다.

    또한 특정 Visual Studio Extension에서는 포함 파일을 검색하기 위한 고유 디렉터리를 지정할 수도 있습니다.예를 들어 Visualization and Modeling SDK(DSL 도구)를 설치한 경우 Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\DSL SDK\DSL Designer\11.0\TextTemplates 폴더가 포함 목록에 추가됩니다.

    이러한 추가적인 포함 폴더는 포함 파일의 파일 확장명에 따라 달라질 수 있습니다.예를 들어 DSL 도구의 포함 폴더에서는 파일 확장명이 .tt인 파일이 있는 포함 파일에만 액세스할 수 있습니다.

  • filePath는 "%"로 구분되는 환경 변수를 포함할 수 있습니다.예를 들면 다음과 같습니다.

    <#@ include file="%HOMEPATH%\MyIncludeFile.t4" #>
    
  • 포함된 된 파일 이름 확장명을 사용 하지 않아도 ".tt".

    같은 다른 확장명을 사용 하는 것이 좋습니다 ".t4" 에 대 한 파일을 포함 합니다.프로젝트에 .tt 파일을 추가하면 Visual Studio에서 사용자 지정 도구 속성이 TextTemplatingFileGenerator로 자동 설정되기 때문입니다.일반적으로 포함된 파일을 개별적으로 변환하지 않을 수 있습니다.

    반면에 파일 확장명에 따라 포함 파일을 검색할 추가 폴더가 영향을 받는 경우가 있다는 사실에 유의해야 합니다.이는 다른 파일이 들어 있는 포함된 파일이 있는 경우 중요할 수 있습니다.

  • 포함된 내용은 포함하는 텍스트 템플릿의 일부인 것처럼 처리됩니다.그러나 include 지시문 뒤에 일반 텍스트와 표준 제어 블록이 있는 경우에도 클래스 기능 블록 <#+...#>이 포함된 파일을 포함할 수 있습니다.

MyTextTemplate.tt:

<#@ output extension=".txt" #>
Output message 1 (from top template).
<#@ include file="TextFile1.t4"#>
Output message 5 (from top template).
<#
   GenerateMessage(6); // defined in TextFile1.t4
   AnotherGenerateMessage(7); // defined in TextFile2.t4
#>

TextFile1.t4:

   Output Message 2 (from included file).
<#@include file="TextFile2.t4" #>
   Output Message 4 (from included file).
<#+ // Start of class feature control block.
void GenerateMessage(int n)
{
#>
   Output Message <#= n #> (from GenerateMessage method).
<#+
}
#>

TextFile2.t4:

        Output Message 3 (from included file 2).
<#+ // Start of class feature control block.
void AnotherGenerateMessage(int n)
{
#>
       Output Message <#= n #> (from AnotherGenerateMessage method).
<#+
}
#>

결과적으로 생성된 파일, MyTextTemplate.txt:

Output message 1 (from top template).
   Output Message 2 (from included file).
        Output Message 3 (from included file 2).

   Output Message 4 (from included file).

Output message 5 (from top template).
   Output Message 6 (from GenerateMessage method).
       Output Message 7 (from AnotherGenerateMessage method).