控制 T4 模板生成的文件名? [英] Control the filename generated by a T4 template?

查看:24
本文介绍了控制 T4 模板生成的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何指示 T4 模板生成具有给定文件名的结果文件?

How can I instruct a T4 template to generate the resulting file with a given file name?

我会尽量说得更清楚.假设我有一个名为 Insert.tt 的模板,它生成用于将记录插入到表中的代码.它的输出扩展名为sql",因此它创建了一个名为:

I'll try to be more clear. Let's say I have a template called Insert.tt that generates code to insert a record into a table. It has an output extension of "sql", so it creates a file called:

插入.sql

没关系,但我希望文件名能够反映我正在尝试做的任何事情.假设我为其生成插入的表的名称存储在 <#= TableName #> 中.我想将它传递给模板生成器并让它使用它来保存生成的文件.

That's OK, but I'd like the file name to reflect whatever it is I'm trying to do. Let's say that the name of the table I'm generating an Insert for is stored in <#= TableName #>. I'd like to pass that to the template generator and have it use that to save the resulting file.

即<# SaveTemplateAs("Insert" + TableName);#>

i.e. <# SaveTemplateAs("Insert" + TableName); #>

假设我为表 User、Address 和 Phone 生成 Insert 语句.

So let's say I generate Insert statements for tables User, Address and Phone.

我希望将生成的文件命名为 InsertUser.sql、InsertAddress.sql 和 InsertPhone.sql.

I want the resulting files to be named InsertUser.sql, InsertAddress.sql and InsertPhone.sql.

我很确定这可以做到,因为我已经看到一些自定义扩展可以从单个模板生成多个文件,但我不理解实现这一点的简单(而不是矫枉过正)的方法.

I'm pretty sure this can be done as I've seen some custom extensions to generate multiple files from a single template, but I'm not understanding a simple (not overkill) way to make this happen.

谢谢

推荐答案

据我所知,T4 中没有内置功能来生成多个输出文件.但是您说得对,有很多示例可以告诉您如何处理多个输出文件.

As far as I know, there is no built-in functionality in T4 to generate multiple output files. But you're right, there are plenty of samples out there that tell you how to deal with multiple output files.

也许这篇博文可以帮助您解决问题:

Maybe this blog post might help you solve your problem:

http://t4-editor.tangible-engineering.com/blog/how-to-generate-multiple-output-files-from-a-single-t4-template.html

此编辑器是免费的,并附带免费示例 - 其中包括将 T4 输出拆分为具有不同名称和/或扩展名的不同文件的代码.

This editor is for free and comes with free samples - one including code to split the T4 output to different files with different names and/or extensions.

这篇关于控制 T4 模板生成的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆