【问题标题】:NVelocity syntax for calling methods with evaluated parameters使用评估参数调用方法的 NVelocity 语法
【发布时间】:2010-07-14 22:54:15
【问题描述】:

我有一个相当简单的模板,我需要从中调用方法。但是,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) 每次都有效)。但是,使用 $var1 会导致整个字符串按原样返回:$extensionMethods.TestMethod(${var1})

有没有办法将一个变量懒惰地传递给一个方法以使上述模板正确评估?

【问题讨论】:

    标签: c# asp.net nvelocity


    【解决方案1】:

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

    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
    

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

    【讨论】:

    • 感谢 Matthew,我花了更多时间修改代码,并意识到我上面提供的示例过于简单。我已经设法修复了代码,但我仍然相信 NVelocity 中存在错误。我将在下面的帖子中解释。
    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 2020-04-21
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2013-08-24
    相关资源
    最近更新 更多