views:

41

answers:

1

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 passing 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?

A: 

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?

Matthew Abbott
Thanks Matthew, I spent some more time tinkering with the code and realized that the example I've provided above is oversimplified. I've managed to fix the code, but I still do believe there is a bug within NVelocity. I'll explain in an post below.
Ishan