Question

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?

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top