Replies: 3 comments
-
Hello @Brandon-VS . Unfortunately not everything can be handled in case-insensitive way. For example consider data object implementing However starting from 2.0 you would be able to handle such problems on you own by registering custom [Fact]
public void CaseInsensitiveExpandoObject()
{
var templateString = "Dear {{displayName}}";
var configuration = new HandlebarsConfiguration();
configuration.CompileTimeConfiguration.ObjectDescriptorProviders.Add(new ExpandoObjectDescriptor());
var handlebars = Handlebars.Create(configuration);
var template = handlebars.Compile(templateString);
var data = new ExpandoObject();
data.TryAdd("DisplayName", "John");
var output = template(data);
Assert.Equal("Dear John", output);
}
public class ExpandoObjectDescriptor : IObjectDescriptorProvider
{
public bool CanHandleType(Type type)
{
return typeof(ExpandoObject).IsAssignableFrom(type);
}
public bool TryGetDescriptor(Type type, out ObjectDescriptor value)
{
if (!typeof(ExpandoObject).IsAssignableFrom(type))
{
value = ObjectDescriptor.Empty;;
return false;
}
value = new ObjectDescriptor(
type,
new ExpandoObjectMemberAccessor(),
(descriptor, o) => ((IDynamicMetaObjectProvider)o).GetMetaObject(Expression.Constant(o)).GetDynamicMemberNames()
);
return true;
}
private class ExpandoObjectMemberAccessor : IMemberAccessor
{
public bool TryGetValue(object instance, Type instanceType, string memberName, out object value)
{
if (instance is ExpandoObject expandoObject)
{
var invariant = memberName.ToUpperInvariant();
var property = ((IDynamicMetaObjectProvider) expandoObject)
.GetMetaObject(Expression.Constant(expandoObject))
.GetDynamicMemberNames()
.FirstOrDefault(o => o.ToUpperInvariant() == invariant);
if (property != null)
{
value = ((IDictionary<string, object>) expandoObject)[property];
return true;
}
}
value = null;
return false;
}
}
} Be aware that proposed API is not final and may change prior to v2 release. |
Beta Was this translation helpful? Give feedback.
-
@zjklee Thank you very much for the quick response and detailed explanation. Now that I understand the behavior a bit more I was able to fix my code. |
Beta Was this translation helpful? Give feedback.
-
For info, I was able to make the model case insensitive by using the following steps: var modelJson = JsonSerializer.Serialize(model);
var caseInsensitiveModel = JsonNode.Parse(modelJson, new JsonNodeOptions() { PropertyNameCaseInsensitive = true });
var compiledTemplate = handlebars.Compile(template);
var result = compiledTemplate(caseInsensitiveModel); |
Beta Was this translation helpful? Give feedback.
-
There are a few places that mention that Handlebars.Net is supposed to be case insensitive as of v1.6.6 but I can't seem to get it to act that way.
Am I doing something wrong in the code? Below is a basic example of what I'm doing
This just results in
Dear
.If I change templateString to be
"Dear {{DisplayName}}"
it would printDear John
This is using the NuGet package v1.10.1 on a .NET Core 3.1 console application.
Beta Was this translation helpful? Give feedback.
All reactions