Control the filename generated by a T4 template?
Asked Answered
E

2

13

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

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:

Insert.sql

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.

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

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

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.

Thanks

Emmerie answered 30/1, 2013 at 15:20 Comment(0)
S
3

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

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.

S answered 31/1, 2013 at 7:57 Comment(0)
C
23

Unfortunately that seems not be possible in the .tt file itself. But you can control output file name in the .csproj file of the project where the text template resides. Unload the project from Visual Studio solution and open the project file e.g. .csproj directly in a text editor.

There you find the reference of the template file.

The node <LastGenOutput> controls the output filename. Here you can choose a file name you want.

Clausen answered 3/9, 2014 at 14:27 Comment(4)
There's no need to unload the project, VS will simply detect that it changed when you save it after hand editing.Michi
@Michi I think it's mildly dangerous to edit it at the same time that VS has it active, which is why the "Edit abc.csproj" option is only available if you unload the project first.Stacystadholder
This is not working for me using VS2019 and runtime templates. It keeps change the name back when regenedSoubrette
Oddly enough, VS may sometimes change the output suffix to a number in the csproj file (probably detects a duplicate or something), so this would also be the way to fix that issue.Unfreeze
S
3

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

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.

S answered 31/1, 2013 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.