Does NVelocity no longer support string templates?
Asked Answered
Z

2

5

We have a bunch of NVelocity templates in embedded resources that we use for emails. We want to move these templates to the DB so they can be configured easily by users.

It seems though that NVelocity (Castle port) doesn't support strings as templates. Does anyone know how to do it.

To be clear this is what I want to do (syntax may be inaccurate, I'm going by memory) ...

string templateString = "Hello $!user";
Template template = new Template(templateString);
string results = template.Merge(....);
Zoila answered 20/2, 2009 at 3:51 Comment(0)
K
15

This works for me:

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() );
}
Khalid answered 18/3, 2009 at 13:52 Comment(2)
The lack of good documentation for NVelocity is depressing because it's such a powerful and useful library. It's taken me far too long to find your answer so I've marked the question as a favourite for future reference and up voted your answer and the question.Heaney
Thank you. However, I do think it's a good idea to look at other template engines, as NVelocity is close to abandoned, quite slow and a tad too limiting.Khalid
Z
0

After much research on my own the Castle port of NVelocity seems like it would be a huge PITA to get the Template from an in memory string.

I've since given up on NVelocity and started use StringTemplate. Particularly this implementation: http://websitelogic.net/articles/MVC/stringtemplate-viewengine-asp-net-mvc/

Zoila answered 22/2, 2009 at 1:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.