Handlebars-Net / Handlebars.Net

A real .NET Handlebars engine
MIT License
1.26k stars 217 forks source link

Updating from v1.11.5 to v2.0.8: RegisterHelper Context no longer dynamic (breaking changes) #464

Open devzaak opened 3 years ago

devzaak commented 3 years ago

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?

2.

output.WriteLine();

no longer exists, what was this replaced with? Thank you

oformaniuk commented 3 years ago

Hello @aslezak

  1. You can use Value property of the Context:

    ...
    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;
  2. You should be able to use Environment.NewLine as 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.

devzaak commented 3 years ago

Great, thanks for the info, I will create the PR for the second part.