Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a method that encapsulates the T4 template text section?

Instead of this .tt:

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ import namespace="System.IO" #>
<#@ output extension=".txt" #>
<#@ assembly name="System"#>

<# message = "hello world" ; #>

blah blah blah etc. very complex example with embedded expression like
<#=message#>

I'd like to have an output function that would return the output blah blah etc.:

    <#@ template debug="false" hostspecific="true" language="C#" #>
    <#@ import namespace="System.IO" #>
    <#@ output extension=".txt" #>
    <#@ assembly name="System"#>

    <#output();#>

   <#+ output() { #>
   blah blah blah etc. very complex example with embedded expression like
    <#=message#>

   <#}
   #>

Of course the syntax above is not correct. How to do this ?

like image 736
user310291 Avatar asked Jan 09 '11 17:01

user310291


1 Answers

This is an alternative solution not using class feature blocks <#+ ... #>. Using a lambda expression inside usual statement blocks <# ... #> allows defining a local function as follows:

<#@ template language="C#" #>
<#@ output extension=".txt" #>

<# Action output = () => { #>
loooooooong text <#= "message" #>
<# }; #>

<# output(); #>

This template produces the output below:

loooooooong text message
like image 148
isaias-b Avatar answered Sep 21 '22 17:09

isaias-b