Unable to cast object of type 'Data' to type 'System.IConvertible' HandleBars DotNet
I really like the idea of using handle bars to create HTML templated content for emails etc as a much neater way of separating presentation from logic and enabling us to store the templates in a simple database and inject data as a separate process.
Anyway, I am starting to use the more advanced features like each and helpers and I got the error above when trying to run my template.
It turns out that it wasn't a HandleBars error as such, I had a helper like this:
and this was called from a template like this:
and on first inspection, I didn't understand what I was soing wrong and why I got such a weird error (rather than simply missing or malformed content. Of course, after looking carefully, the line of code that was actually erroring was:
And it turns out that since parameters are dynamic, I realised that AverageScore was not the correct name of the data parameter but it should have been Average.Without it being a valid piece of data, the helper simply injected the string "AverageScore" which, naturally, would not covert to a decimal.
Fun times!
Anyway, I am starting to use the more advanced features like each and helpers and I got the error above when trying to run my template.
It turns out that it wasn't a HandleBars error as such, I had a helper like this:
Handlebars.RegisterHelper("FormatNumber", (writer, context, parameters) => {
writer.WriteSafeString(Convert.ToDecimal(parameters[0]).ToString("F2"));
});
and this was called from a template like this:
"{{FormatNumber AverageScore}}"
and on first inspection, I didn't understand what I was soing wrong and why I got such a weird error (rather than simply missing or malformed content. Of course, after looking carefully, the line of code that was actually erroring was:
Convert.ToDecimal(parameters[0])
And it turns out that since parameters are dynamic, I realised that AverageScore was not the correct name of the data parameter but it should have been Average.Without it being a valid piece of data, the helper simply injected the string "AverageScore" which, naturally, would not covert to a decimal.
Fun times!