T4 Toolbox - mixing class feature and statement blocks
Asked Answered
L

3

14

I'm a T4 newbie trying to use T4 Toolbox to generate F# code based on this answer, but it seems that class feature blocks can't be mixed with statement blocks. Here's my code:

<#@ template language="C#" hostspecific="True" debug="True" #>
<#@ output extension="txt" #>
<#@ include file="T4Toolbox.tt" #>
<#
    FSharpTemplate template = new FSharpTemplate();
    template.Output.Project = @"..\Library1\Library1.fsproj";
    template.Output.File = "Module2.fs";
    template.Render();
#>
<#+
class FSharpTemplate: Template
{
    public override string TransformText()
    {
#>

module Module2

<# for (int i = 0; i < 10; i++) { #>
<#= i #>
<# } #>

<#+
        return this.GenerationEnvironment.ToString();
    }
}

#>

And I get this error:

A Statement cannot appear after the first class feature in the template. Only boilerplate, expressions and other class features are allowed after the first class feature block.

So... how can I rewrite the template to achieve this?

Lillielilliputian answered 17/4, 2010 at 21:8 Comment(0)
C
10

After the first class feature block, you need to make all the subsequent statement blocks also class feature blocks.

Under the covers, the first class feature block terminates the behind the scenes "Generate" method and switches to inserting the content as members of the template's behind the scenes class.

If you're using Visual Studio 2010, you can always create a preprocessed template and paste your regular template code into that to see what's going on under the hood.

Congress answered 1/5, 2010 at 20:38 Comment(0)
S
8

While @GarethJ's answer explains why this happens, it does not tell you how to fix it. You need to add a plus sign, i.e. use <#+ instead of just <#

<#+ for (int i = 0; i < 10; i++) { #>
<#= i #>
<#+ } #>
Suckow answered 13/2, 2018 at 17:22 Comment(0)
B
4

You should have all class features in the same feature block, below any output.

Blindfish answered 17/4, 2010 at 21:52 Comment(2)
If I do that I get no output at allLillielilliputian
I see. I don't really understand how the answer in the other question was intended to work, but I can tell you that this is a rule for T4. I would recommend to add a link to the generated file in the other project.Blindfish

© 2022 - 2024 — McMap. All rights reserved.