What is the '.tt' extension?
Asked Answered
W

4

17

I work with a bunch of something.js.tt JavaScript files using Knockout and a bunch of something-else.tt HTML files.

The infrastructure is mostly a C backend with Perl serving API and we use these .tt files to show the HTML and .js.tt to serve the Knockout.js code. What is .tt?

Willettewilley answered 5/1, 2016 at 21:17 Comment(0)
G
20

A TT file is a Visual Studio Text Template, developed by Microsoft.

Gerdi answered 5/1, 2016 at 21:21 Comment(0)
E
16

Text Template Transformation Toolkit, shortly written as T4, uses the .tt file extension for its source files. It is Microsoft's template-based text generation framework included with Visual Studio.

For more info, see the docs.

Elastomer answered 12/11, 2017 at 18:49 Comment(0)
A
3

If you take a look inside the file, you'll probably notice a lot of logic injecting things. This is because this kind of files are used to generate other files.

As explained in the MS page shared by @Recev Yildiz:

In Visual Studio, a T4 text template is a mixture of text blocks and control logic that can generate a text file.

The control logic is written as fragments of program code in Visual C# or Visual Basic. In Visual Studio 2015 Update 2 and later, you can use C# version 6.0 features in T4 templates directives.

The generated file can be text of any kind, such as a web page, or a resource file, or program source code in any language.

There are two kinds of T4 text templates: run time and design time.

Here's an example of a code I've got from a Entity Framework file, from a ASP.NET Web Application (.NET Framework) project (MVC design):

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF6.Utility.CS.ttinclude"#><#@
 output extension=".cs"#><#

const string inputFile = @"DBModel.edmx";
var textTransform = DynamicTextTransformation.Create(this);
var code = new CodeGenerationTools(this);
var ef = new MetadataTools(this);
var typeMapper = new TypeMapper(code, ef, textTransform.Errors);
var loader = new EdmMetadataLoader(textTransform.Host, textTransform.Errors);
var itemCollection = loader.CreateEdmItemCollection(inputFile);
var modelNamespace = loader.GetModelNamespace(inputFile);
var codeStringGenerator = new CodeStringGenerator(code, typeMapper, ef);

var container = itemCollection.OfType<EntityContainer>().FirstOrDefault();
if (container == null)
{
    return string.Empty;
}
#>
//------------------------------------------------------------------------------
// <auto-generated>
// <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine1")#>
//
// <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine2")#>
// <#=CodeGenerationTools.GetResourceString("Template_GeneratedCodeCommentLine3")#>
// </auto-generated>
//------------------------------------------------------------------------------

<#

var codeNamespace = code.VsNamespaceSuggestion();
if (!String.IsNullOrEmpty(codeNamespace))
{
#>
namespace <#=code.EscapeNamespace(codeNamespace)#>
{
<#
    PushIndent("    ");
}

#>
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
<#
if (container.FunctionImports.Any())
{
#>
using System.Data.Entity.Core.Objects;
using System.Linq;
<#
}
#>

<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
{
    public <#=code.Escape(container)#>()
        : base("name=<#=container.Name#>")
    {
<#
if (!loader.IsLazyLoadingEnabled(container))
{
#>
        this.Configuration.LazyLoadingEnabled = false;
<#
}

foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())
{
    // Note: the DbSet members are defined below such that the getter and
    // setter always have the same accessibility as the DbSet definition
    if (Accessibility.ForReadOnlyProperty(entitySet) != "public")
    {
#>
        <#=codeStringGenerator.DbSetInitializer(entitySet)#>
<#
    }
}
#>
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

<#
    foreach (var entitySet in container.BaseEntitySets.OfType<EntitySet>())
    {
#>
    <#=codeStringGenerator.DbSet(entitySet)#>
<#
    }

    foreach (var edmFunction in container.FunctionImports)
    {
        WriteFunctionImport(typeMapper, codeStringGenerator, edmFunction, modelNamespace, includeMergeOption: false);
    }
#>
}

The file was way larger than what you see here. And as you can see, it seems to be a really busy code.

This is the context where the file is placed:

enter image description here

Admissible answered 2/8, 2021 at 16:10 Comment(0)
S
0

TT stands for - Visual Studio Text Template is a software development tool created by the Microsoft.

Further explanation - TT file contains text block and control logic used for generating new files. To write the Text Template file we can use either - Visual C# or Visual Basic Code

It's mainly used for handling Run Time text generation and source code generation both at once. They're like normal text files and can be viewed in any text editor.

Surreptitious answered 2/6, 2022 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.