string templateText = File.ReadAllText(Host.ResolvePath(templateFileName));
Engine engine = new Engine();
string output = engine.ProcessTemplate(templateText, Host);
//this is optional - record all output to your file of choice:
File.WriteAllText(outputFilePath, output);
We do this a lot. Here is an example how we reuse a common T4 template yet "pass parameters" into it:
<#
var currentUsername = "billclinton"; // this is for integration tests impersonating a user in our case
#>
<#@ include file="..\SomeTemplateThatIWantToReuseHere.tt" #>
And we keep our T4 template "generic" by dynamically determining the location that the T4 template is actually ran in (in this case, the T4 template that has the include line in it):
string namespaceName = code.VsNamespaceSuggestion();
var namespaceParts = namespaceName.Split('.');
var currentNamespaceLeg = namespaceParts.Last();
This allows us to do some very powerful templating without the need to duplicate our templates. The only thing being "duplicated" are our 4-line .tt files that have the include call in them, but these are pretty much maintenance free except whatever "configuration" we want to perform by passing in variables in the way we do it.
What your probably looking for is http://t4toolbox.codeplex.com/ t4 Toolbox. It will allow you to actually generate code in individual files and add them to a project automatically.
Highly Recommended.
I have used t4 toolbox to generate whole projects just based on a model.
Yes, you can. This is how I'm doing it:
We do this a lot. Here is an example how we reuse a common T4 template yet "pass parameters" into it:
And we keep our T4 template "generic" by dynamically determining the location that the T4 template is actually ran in (in this case, the T4 template that has the
include
line in it):This allows us to do some very powerful templating without the need to duplicate our templates. The only thing being "duplicated" are our 4-line
.tt
files that have theinclude
call in them, but these are pretty much maintenance free except whatever "configuration" we want to perform by passing in variables in the way we do it.What your probably looking for is http://t4toolbox.codeplex.com/ t4 Toolbox. It will allow you to actually generate code in individual files and add them to a project automatically.
Highly Recommended.
I have used t4 toolbox to generate whole projects just based on a model.
There are multiple optons with different trade-offs:
http://www.olegsych.com/2008/04/t4-template-design/