tags:

views:

145

answers:

2

I'm starting to investigate T4 for Code Generation.

I get that you have a basic template in which you can embed little chunks of c#/vb which can perform clever stuff...

<#@ template language="VB" debug="True" hostspecific="True" #>
<#@ output extension=".vb" debug="True" hostspecific="True" #>
Imports System
<#For Each Table as String in New String(0 {"Table1","Table2"}#>
Public Class <#=Table#>DA
    Public Sub New
        <#= WriteConstructorBody() #>
    End Sub 
End Class
<#Next#>
<#+
    Public Function WriteConstructorBody() as String
        return "' Some comment"
    End function
#>

This is great.. However I would like to be able to write my main block thus...

<#@ template language="VB" debug="True" hostspecific="True" #>
<#@ output extension=".vb" debug="True" hostspecific="True" #>
Imports System
<# 
For Each BaseTableName as String in New String(){"Table1","Table2"} 
    WriteRecDataInterface(BaseTableName) 
    WriteRecDataClass(BaseTableName) 
    WriteDAInterface(BaseTableName) 
    WriteDAClass(BaseTableName) 
Next 
#>

Then I would like to be able to have the WriteX methods exist in a Class Block but themselves be writable using code by example ie escaped Code blocks.

How can I achieve this?

+1  A: 

It seems that you can mix static output with template code in Class Blocks. Here is an example with C#:

<#@ template language="C#" #>
<# HelloWorld(); #>
<#+
    private string _field = "classy";
    private void HelloWorld()
    {
        for(int i = 1; i <= 3; i++)
        {
#>
Hello <#=_field#> World <#= i #>!
<#+
        }
    }
#>
Enrico Campidoglio
I had to look hard to locate the static text in your example.... for me the <#= stuff was confusing matters.
Rory Becker
A: 

You can write.....

<#@ template language="VB" debug="True" hostspecific="True" #>
<#@ output extension=".vb" debug="True" hostspecific="True" #>
Imports System
<# 
For Each BaseTableName as String in New String(){"Table1","Table2"} 
    WriteRecDataInterface(BaseTableName) 

    ' WriteRecDataClass(BaseTableName) 
    ' WriteDAInterface(BaseTableName) 
    ' WriteDAClass(BaseTableName) 
Next 
#>


<#+ Public Sub WriteRecDataInterface(BaseTableName as String)#>
    Some Templated unescaped code might go here
    <#+ For SomeLoopVar as Integer = 1 to 10 #>
        Some Templated unescaped code might go here
    <#+ Next #>
    Some Templated unescaped code might go here
<#+ End Sub #>
'...
'...
' Other Subs left out for brevity
'...
Rory Becker