Domanda

I try to pass a parameter(foo) to an existing template(MyTemplate.tt) with this command:

TextTransform.exe -a=foo!bar -o Output.txt MyTemplate.tt

MyTemplate.tt:

<#@ template language="C#" #>
<#@ import namespace="System.IO" #>

<#
  this.Write(foo);  
#>

This command fails due to the parameter "foo" is not existing. How can I pass this parameter correctly?

È stato utile?

Soluzione

The arguments of MonoDevelop's TextTransform.exe match Microsoft's, which are documented here.

Unfortunately, these parameters are not exposed as as variables in code, they are intended as arguments for custom directive processors. To access them directly from code you must set hostspecific="true" and access them via Host.ResolveParameterValue (paramName).

You could however use dynamic objects to make your example work, by implementing IDynamicMetaObjectProvider on your template's base class and overriding BindGetMember to resolve parameters from Host.ResolveParameterValue.

Altri suggerimenti

Thanks, this works fine for me:

TextTransform.exe -a=foo!bar -o Output.txt MyTemplate.tt

MyTemplate.tt:

<#@ template language="C#" hostspecific="true" #>
<# string temp = this.Host.ResolveParameterValue("", "", "foo");#>
<#
  this.Write(temp);  
#>

Output.txt:

bar

I will have also a look at IDynamicMetaObjectProvider.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top