用于调用带有评估参数的方法的 NVelocity 语法

发布于 2024-09-09 04:16:53 字数 651 浏览 1 评论 0原文

我有一个相当简单的模板,我需要从中进行方法调用。但是,NVelocity 似乎并不评估本身就是 NVelocity 变量 的方法参数。采用以下 NVelocity 模板:

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

在上面的模板中,$extensionMethods 作为类的实例传入,并且在传入计算值时效果非常好(例如 $extensionMethods.TestMethod(4)< /code> 每次都有效)。但是,使用 $var1 会导致整个字符串按原样返回:$extensionMethods.TestMethod(${var1})

有没有办法将变量延迟传递给方法以使上述模板正确评估?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2024-09-16 04:16:53

如果您遇到问题,可能与您的变量类型或方法可用性有关。我已经构建并测试了以下内容:

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

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

和我的模板:

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

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

和输出:

Name: "MATT"
Age: 100

我们可以看到您的扩展方法的一些代码吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文