views:

139

answers:

1

By basic T4 template, I mean not using T4 Toolkit or any of the add-ins.

My T4 is getting a little complicated, but I'd like to keep in self-contained for now. Is there a way have functions in your T4 template, without referencing external assemblies?

+1  A: 

You mean like this:

<#+
    public List<string> Dostuff()
    {
        List<string> result = new List<string>(); 

        // ...

        return result;
    }
#>

Here's a complete example: Reading a Xml File in T4 Templates

Mitch Wheat
"<#+ #>", excellent, that's what I'm after. Do you know, can you define types in that *<#+* code block?
Patrick Karcher
Yes, you can define types. The <#+ #> is called a "class feature block" in T4, which means it becomes part of the underlying class that generates your output. So, if you define any members, they also become part of that class. See http://www.olegsych.com/2008/02/t4-class-feature-blocks/ for more details
Chris Melinn
@Chris Melinn: great link. I was trying to remember oleg's name yesterday!
Mitch Wheat
@Mitch Wheat. Yes, his blog is a fantastic resource for T4
Chris Melinn