Show Menu
Cheatography

T4 Templates Cheat Sheet by

Cheat Sheet for Microsoft T4 Templates

T4 Extensions

Microsoft Docume­ntation on T4

Template Header

Define Language and Debugging
<#@ template debug=­"­fal­se" hostsp­eci­fic­="tr­ue" langua­ge=­"­C#" #>
Define File Extension
<#@ output extens­ion­=".c­s" #>
Reference Built In Assembly
<#@ assembly name="S­yst­em.C­or­e" #>
Reference External Assembly
<#@ assembly name="$­(pr­oje­ctD­ir)­\As­sem­bli­es­\Kel­ler­man­Sof­twa­re.N­ET­-Co­de-­Gen­era­tor­-Lo­gic.dl­l" #>
Include Another Template
<#@ include file="$­(pr­oje­ctD­ir)­\In­clu­des­\CS­har­pHe­lpe­r.t­t" #>

Control Blocks

<#
    for(int i = 0; i < 4; i++)
    {#>
          This is index <#= i+1 #>
<# }#>
 

Videos on T4 Templates

Text Template Utility Methods

WriteL­ine­("Some Text")
Write(­"Some Text")
PushIn­den­t("­\t")
ClearI­ndent()

Class Feature Block

<#+
    private string GetTemplateDirectory()
    {
        return Path.GetDirectoryName(Host.TemplateFile);
    }

    private void SaveOutput(string outputFileName, string subDirectory= "") {
        string templateDirectory = GetTemplateDirectory();
        if (!string.IsNullOrEmpty(subDirectory))
        {
            templateDirectory = Path.Combine(templateDirectory, subDirectory);
        }
        string outputFilePath = Path.Combine(templateDirectory, outputFileName);

        if (!Directory.Exists(templateDirectory))
        {
            Directory.CreateDirectory(templateDirectory);
        }

        File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());
        this.GenerationEnvironment.Clear();
    }
#>
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          More Cheat Sheets by GregFinzer

          RhinoMocks Cheat Sheet
          Salesforce CLI Cheat Sheet