Control the file name generated by the T4 template?

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

I will try to be more clear. Say I have a template called Insert.tt that generates code to insert a record into a table. It has the output extension "sql", so it creates a file with the name:

Insert.sql

This is fine, but I would like the file name to reflect what I'm trying to do. Let's say that the name of the table in which I create Insert for is stored in <# = TableName #>. I would like to pass this to the template generator and use it to save the resulting file.

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

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

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

I am sure that this can be done, since I saw some custom extensions for generating several files from one template, but I do not understand a simple (not too big) way to do this.

thanks

+8
t4
source share
2 answers

As far as I know, in T4 there are no built-in functions for generating multiple output files. But you're right, there are many samples that tell you how to handle multiple output files.

Perhaps this blog post may help you solve your problem:

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

This editor is free and comes with free samples - one of which includes code for splitting T4 output into different files with different names and / or extensions.

+1
source share

Unfortunately, this is not possible in the .tt file .tt . But you can control the name of the output file in the project .csproj file in which the text template is located. Unload the project from the Visual Studio solution and open the project file, for example. .csproj directly in a text editor.

Here you will find a link to the template file.

The node element <LastGenOutput> controls the output file name. Here you can select the desired file name.

+18
source share

All Articles