Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does NVelocity no longer support string templates?

Tags:

nvelocity

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(....);
like image 341
Kyle West Avatar asked Feb 20 '09 03:02

Kyle West


1 Answers

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() );
}
like image 191
Bergius Avatar answered Sep 17 '22 08:09

Bergius