This looks like a defect with NVelocity's handling of .NET's Dictionary<K,V>
. Because of NVelocity's origins in Velocity before Java supported generics and because NVelocity is an old codebase, I tried with a non-generic Hashtable
and it works as expected. Since the map isn't typed in the NVelocity template, it should be a drop in change to switch classes to workaround this defect.
Feel free to log the defect, but without a pull request it is very unlikely to be fixed.
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.Init();
Hashtable dict = new Hashtable();
dict.Add("customValue1", "mydata");
VelocityContext context = new VelocityContext();
context.Put("dict", dict);
using (StringWriter sw = new StringWriter())
{
velocityEngine.Evaluate(context, sw, "",
"$dict.get_Item('customValue1')\r\n" +
"$dict.get_Item('customValue2')\r\n" +
"$!dict.get_Item('customValue2')"
);
Assert.AreEqual(
"mydata\r\n" +
"$dict.get_Item('customValue2')\r\n" +
"",
sw.ToString());
}