T 4 beginners – part 4
this is the 4rd post on this series.
in this post we will focus on T4 Class feature control blocks.
you can download the code for this post here.
the series TOC is available here.
What is class feature control block?
class feature control block is one of the T4 extension mechanism that enable reuse of
T4 sections.
using class feature control block you can defines helper properties or methods,
that can be used from different T4 templates.
The syntax of class feature control block
class feature control block is having the following syntax <#+ … #>
- <#+ // Start of class feature block
- private int Square(int i)
- {
- return i*i;
- }
- #>
this extension can be use within control block or expression control block, as demonstrate:
- // This is hello number <#= i+1 #>, square <#= Square(i+1) #>
as you can the second expression block is using the Square (class feature method).
Where class feature control block can be define?
class feature control block can be both inline (which mean inside the T4 template)
or external (external helper file), which is more appropriate.
How to define external class feature control block?
to define class feature control block in external file, you should:
1. add text file into your project
(I’m changing the text file extension to *.tt.extension, but you can use whatever extension you like).
update: it is better to name the file extension *ttinclude
2. the class feature control block should be added into the extension file.
How to include external class feature control block?
if you want to use external class feature control block you should add
include directive to the T4 as demonstrate below:
Summary
class feature control block is one of the strength, you can simplify the T4 template
by using predefine reusable methods.
Code sample available here.