Handlebars.Net
Handlebars.Net copied to clipboard
Updating from v1.11.5 to v2.0.8: RegisterHelper Context no longer dynamic (breaking changes)
Hi, In recent updates I saw in notes that context when registering helpers context is no longer dynamic and I need just a bit of help to understand how to change my code to work with new version. There are 2 issues: 1.
Handlebars.RegisterHelper("key", (output, context, arguments) =>
{
if (arguments.Length != 1)
throw new HandlebarsException("Missing argument key path; {{key '<key-path>'}}");
if (!(context is Dictionary<string, string> configuration)) return; // ---> cast no longer valid
if (!configuration.TryGetValue(ConfigKey(arguments[0]), out var value)) return;
output.Write(value);
});
How should I be working with context in this case to get dictionary back?
output.WriteLine();
no longer exists, what was this replaced with? Thank you
Hello @aslezak
- You can use
Valueproperty of theContext:
...
if (!(context.Value is Dictionary<string, string> configuration)) return;
...
or access value via Context:
var value = context.GetValue<string>(arguments.At<string>(0));
if (value == null) return;
- You should be able to use
Environment.NewLineas a workaround (probably even wrap it in an extension method):
output.Write(value);
output.Write(Environment.NewLine, false);
I'd be glad to accept a PR for this missing part.
Great, thanks for the info, I will create the PR for the second part.