Adding a dictionary with values like this:
Dictionary<string, string> CustomArray = new Dictionary<string, string>();
CustomArray.Add("customValue1", "mydata");
this.velocityContext.Put("array", CustomArray);
Using the template engine like this:
Velocity.Init();
string template = FileExtension.GetFileText(templateFilePath);
var sb = new StringBuilder();
using(StringWriter sw = new StringWriter(sb))
{
using(StringReader sr = new StringReader(template))
{
Velocity.Evaluate(
this.velocityContext,
sw,
"test template",
sr);
}
}
return sb.ToString();
Accessed in template like this:
$array.Get_Item('customValue1')
$array.Get_Item('customValue2')
customValue1 is retrieved fine, but customValue2 is throwing an KeyNotFoundException because the key does not exists in the dictionary. How can I still generate the template without removing the line that throws KeyNotFoundException?
I've looked at the Apache Velocity guideline but I'm not sure how to append this (https://velocity.apache.org/tools/devel/creatingtools.html#Be_Robust)