Closed StefH closed 1 year ago
How is this different from build-in lookup
helper?
@zjklee Ah. I understand. There's a builtin helper. But that helper does not allow for 3 arguments with a defaultValue.
So a change could be like:
using HandlebarsDotNet.PathStructure;
namespace HandlebarsDotNet.Helpers
{
public sealed class LookupReturnHelperDescriptor : IHelperDescriptor<HelperOptions>
{
public PathInfo Name { get; } = "lookup";
public object Invoke(in HelperOptions options, in Context context, in Arguments arguments)
{
if (arguments.Length != 2 && arguments.Length != 3)
{
throw new HandlebarsException("{{lookup}} helper must have two or three arguments");
}
var segment = ChainSegment.Create(arguments[1]);
var defaultValueIfNotFound = arguments.Length == 3 ? arguments[2] : UndefinedBindingResult.Create(segment);
return !options.TryAccessMember(arguments[0], segment, out var value)
? defaultValueIfNotFound
: value;
}
public void Invoke(in EncodedTextWriter output, in HelperOptions options, in Context context, in Arguments arguments)
{
output.Write(Invoke(options, context, arguments));
}
}
}
@StefH , looks good to me 🙂
@zjklee Shall I create a PR for Handlebars.Net ?
@zjklee See PR https://github.com/Handlebars-Net/Handlebars.Net/pull/542
Codecov Report
0.00% <0.00%> (ø)
57.25% <ø> (+4.62%)
80.00% <ø> (ø)
25.92% <31.57%> (+12.88%)
18.40% <76.19%> (+5.36%)
82.07% <90.90%> (+1.17%)
66.66% <100.00%> (ø)
100.00% <100.00%> (ø)
100.00% <100.00%> (ø)
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.