【问题标题】:Does NVelocity no longer support string templates?NVelocity 是否不再支持字符串模板?
【发布时间】:2009-02-20 03:51:01
【问题描述】:

我们在用于电子邮件的嵌入式资源中有一堆 NVelocity 模板。我们希望将这些模板移动到数据库中,以便用户可以轻松配置它们。

NVelocity(Castle 端口)似乎不支持字符串作为模板。有谁知道该怎么做。

要明确这是我想要做的(语法可能不准确,我会根据记忆进行)...

string templateString = "Hello $!user";
Template template = new Template(templateString);
string results = template.Merge(....);

【问题讨论】:

    标签: nvelocity


    【解决方案1】:

    这对我有用:

    using System.Collections;
    using System.IO;
    using NUnit.Framework;
    using NVelocity;
    using NVelocity.App;
    
    [Test]
    public void StringParsing()
    {
        var h = new Hashtable {
            { "foo", "Template" },
            { "bar", "is working" },
            { "foobar", new[] { "1", "2", "3" } } };
        Velocity.Init();
        var c = new VelocityContext( h );
        var s = new StringWriter();
        Velocity.Evaluate( c, s, "",
            "$foo $bar: #foreach ($i in $foobar)$i#end" );
        Assert.AreEqual( "Template is working: 123", s.ToString() );
    }
    

    【讨论】:

    • NVelocity 缺乏好的文档令人沮丧,因为它是如此强大和有用的库。我花了很长时间才找到你的答案,所以我已将该问题标记为收藏以供将来参考,并对你的答案和问题投了赞成票。
    • 谢谢。但是,我确实认为看看其他模板引擎是个好主意,因为 NVelocity 已接近废弃,速度很慢,而且限制太多。
    【解决方案2】:

    经过我自己的大量研究,NVelocity 的 Castle 端口似乎从内存中的字符串中获取模板将是一个巨大的 PITA。

    我已经放弃了 NVelocity 并开始使用 StringTemplate。特别是这个实现:http://websitelogic.net/articles/MVC/stringtemplate-viewengine-asp-net-mvc/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多