开发者

NVelocity syntax for calling methods with evaluated parameters

I have a fairly simple template that I need to make a method call from. However, NVelocity does not seem to evaluate method parameters that themselves are NVelocity variables. Take the following NVelocity template:

#if (--- CONDITION SNIPPED ---)
    <blockquote class="column span-4">
          I MADE IT!
    </blockquote>
#else
    <blockquote class="column span-4">
         $extensionMethods.TestMethod(${var1})
</blockquote>       
#end

In the above template, $extensionMethods is passed in as an instance of a class and works wonderfully when pa开发者_JAVA技巧ssing in evaluated numbers (e.g. $extensionMethods.TestMethod(4) works every time). However, using $var1 causes the entire string to be returned as-is: $extensionMethods.TestMethod(${var1}).

Is there a way to pass in a variable to a method lazily to get the above template to evaluate correctly?


If you are having trouble, it's likely to be something to do with your variable types, or the method availability. I've built and tested the following:

public class TestClass
{
    #region Methods
    public string DoSomething(string name)
    {
        return name.ToUpperInvariant();
    }

    public string DoSomethingElse(int age)
    {
        return (age*10).ToString();
    }
    #endregion
}

And my template:

#set($myVar = "matt")
#set($myVar2 = 10)

Name: $test.DoSomething(${myVar})
Age: $test.DoSomethingElse(${myVar2})

And the output:

Name: "MATT"
Age: 100

Can we see some code for your extension methods?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜