Code Generation and T4 Text Templates
26-Mar-1111 Leave a comment
http://msdn.microsoft.com/en-us/library/bb126445.aspx
http://brochure.jrcs3.com/2011/03/t4-template-tour-of-first-gear.html
Include directive
Quite useful
<#@ include file=”TextTemplateVB6.t4″ #>
<#@ include File=”$(SolutionDir)\CodeDBGenerator\TextTemplateLibrary\TextTemplate\GeneratedHeader.t4″ #>
This uses actual file location, which may be different to how it appears in Solution explorer.
Note: the included file cannot have a <#@ template language=”VB” #> on it.
Probably want to rename it .t4 and remove the custom tool, so that it does not generate it’s own file.
After it has been included then Class Feature Blocks may be put in here for re-use by many templates. <#+
If you want it separately and also included elsewhere then a strategy I came up with was to have have the file without the <#@ template language=”VB” #> and then a separate file which was just:
<#@ template language=”VB” #>
<#@ include file=”TextTemplateVB6.t4″ #>
Be careful on .t4 file, you probably want to not have a custom tool. This can be done by creating a standard text file and changing it’s file extenstion to .t4 or create a text template file and then remove the Custom Tool from files properties. See: Run Custom Tool
To use T4 within a program
To have a form that generates code say in a text box. Then:
- Set “Custom Tool” of text template to “TextTemplatingFilePreprocessor” and Custom Tool Namespace to say “My.Templates”
Dim code = New My.Templates.PreTextTemplate1
GeneratedCodeTextBox.Text = code.TransformText
To use If.
For instance with include file then:
<# If ParameterList(4) = 1 Then #>
<#@ include file=”SetRowBase.tt” #>
<# End If #>
More blog TODO
… looks potentially useful in some circumstances
Directives
http://msdn.microsoft.com/en-us/library/bb126421.aspx
To read
http://msdn.microsoft.com/en-us/library/gg251243.aspx