“.tt”扩展名是什么?

16

我使用 Knockout 和一组 something.js.tt JavaScript 文件以及一组 something-else.tt HTML 文件。

基础设施主要是 C 后端,Perl 用于提供 API,我们使用这些 .tt 文件来显示 HTML ,并使用 .js.tt 提供 Knockout.js 代码。那么,.tt 是什么?

4个回答

19

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


16

文本模板转换工具包,简称T4,使用.tt文件扩展名作为其源文件。它是微软公司基于模板的文本生成框架,包含在Visual Studio中。

了解更多信息,请参见文档


3

如果你查看这个文件的内部,你可能会注意到很多逻辑注入的内容。这是因为这种类型的文件被用来生成其他文件。

如@Recev Yildiz分享的微软页面所解释的:

在Visual Studio中,T4文本模板是一种混合了 文本块控制逻辑 可以生成文本文件的东西。

控制逻辑是用Visual C#或Visual Basic编写的程序代码片段。在Visual Studio 2015 Update 2和更新版本中,您可以在T4模板指令中使用C#6.0版本特性。

生成的文件可以是任何类型的文本,例如Web页面、资源文件或任何语言的程序源代码。

有两种T4文本模板:运行时和设计时。

以下是我从一个ASP.NET Web应用程序(.NET Framework)项目(MVC设计)的Entity Framework文件中获取的代码示例:

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

这个文件比你在这里看到的要大得多。正如你所看到的,代码似乎非常繁忙。
这是文件所在的上下文:
enter image description here

0

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

进一步解释 - TT文件包含用于生成新文件的文本块控制逻辑。编写Text Template文件可以使用Visual C#Visual Basic Code

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接