“.tt”扩展名是什么?

What is the '.tt' extension?

我使用 Knockout 处理一堆 something.js.tt JavaScript 文件和一堆 something-else.tt HTML 文件。

基础设施主要是 C 后端,Perl 服务 API,我们使用这些 .tt 文件来显示 HTML 和 .js.tt 来服务 Knockout.js 代码。什么是 .tt

TT 文件是 Visual Studio 文本模板,由 Microsoft 开发。

Text Template Transformation Toolkit,简称为 T4,其源文件使用 .tt 文件扩展名。它是 Visual Studio.

中包含的 Microsoft 基于模板的文本生成框架

有关详细信息,请参阅 the docs

如果您查看文件内部,您可能会注意到很多逻辑注入的东西。这是因为这种文件是用来生成其他文件的。

如@Recev Yildiz 分享的 MS 页面中所述:

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.

这是我从 Entity Framework 文件中获得的代码示例,来自 ASP.NET Web 应用程序(.NET Framework)项目(MVC 设计):

<#@ 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);
    }
#>
}

该文件比您在此处看到的要大得多。正如您所看到的,它似乎是一个非常繁忙的代码。

这是放置文件的上下文:

TT 代表 - Visual Studio Text Template 是 Microsoft 创建的软件开发工具。

进一步说明 - TT 文件包含用于生成新文件的 text blockcontrol logic。要编写文本模板文件,我们可以使用 - Visual C#Visual Basic Code

主要用于同时处理运行时间文本生成源代码生成。它们就像普通的文本文件,可以在任何文本编辑器中查看。