而不是这个.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#>我希望有一个输出函数,可以返回输出等等:
<#@ 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#>
<#}
#>当然上面的语法是不正确的。该怎么做呢?
发布于 2014-05-24 08:40:16
这是一个使用类功能块<#+ ... #>而不是的替代解决方案。在通常的语句块<# ... #>中使用lambda表达式允许按如下方式定义局部函数:
<#@ template language="C#" #>
<#@ output extension=".txt" #>
<# Action output = () => { #>
loooooooong text <#= "message" #>
<# }; #>
<# output(); #>此模板生成以下输出:
loooooooong text message发布于 2011-01-20 10:18:19
实际上,你已经非常接近你所拥有的东西了。我发现记住这个模板本质上是一个隐藏在幕后的C#/VB类是很有帮助的,所以当您使用<#+ #>块时,您实际上只是向该类添加了一个成员。
一旦开始使用<#+ #>表示法,就必须继续使用它,因为您仍然是在成员级别向类添加内容,而不是像常规的<# #>标记那样添加TransformText()方法。
正确的语法应该是
<#+ public void output() { #>
blah blah blah etc. very complex example with embedded expression like <#=message#>
<#+ }
#>https://stackoverflow.com/questions/4640623
复制相似问题