diff --git a/source/Handlebars.Test/ComplexIntegrationTests.cs b/source/Handlebars.Test/ComplexIntegrationTests.cs
index 19554726..d168838a 100644
--- a/source/Handlebars.Test/ComplexIntegrationTests.cs
+++ b/source/Handlebars.Test/ComplexIntegrationTests.cs
@@ -90,6 +90,36 @@ public void IfImplicitIteratorHelper()
Assert.Equal("GoogleYahoo!", result);
}
+ // the helper has priority
+ // https://handlebarsjs.com/guide/expressions.html#disambiguating-helpers-calls-and-property-lookup
+ [Fact]
+ public void HelperWithSameNameVariable()
+ {
+ Handlebars.RegisterHelper("foo", (writer, context, arguments) =>
+ {
+ writer.Write("Helper");
+ });
+
+ var template = Handlebars.Compile("{{foo}}");
+ var result = template(new { foo = "Variable" });
+ Assert.Equal("Helper", result);
+ }
+
+ [Fact]
+ public void LateBoundHelperWithSameNameVariable()
+ {
+ var template = Handlebars.Compile("{{amoeba}}");
+
+ Assert.Equal("Variable", template(new { amoeba = "Variable" }));
+
+ Handlebars.RegisterHelper("amoeba", (writer, context, arguments) =>
+ {
+ writer.Write("Helper");
+ });
+
+ Assert.Equal("Helper", template(new { amoeba = "Variable" }));
+ }
+
[Fact]
public void BlockHelperWithSameNameVariable()
{
diff --git a/source/Handlebars/Compiler/Translation/Expression/HelperFunctionBinder.cs b/source/Handlebars/Compiler/Translation/Expression/HelperFunctionBinder.cs
index 1205ea9e..4bca8b1c 100644
--- a/source/Handlebars/Compiler/Translation/Expression/HelperFunctionBinder.cs
+++ b/source/Handlebars/Compiler/Translation/Expression/HelperFunctionBinder.cs
@@ -1,8 +1,8 @@
-using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Collections.Generic;
+using System.IO;
namespace HandlebarsDotNet.Compiler
{
@@ -32,27 +32,31 @@ protected override Expression VisitStatementExpression(StatementExpression sex)
protected override Expression VisitHelperExpression(HelperExpression hex)
{
- if (CompilationContext.Configuration.Helpers.ContainsKey(hex.HelperName))
+ var arguments = new Expression[]
{
- var helper = CompilationContext.Configuration.Helpers[hex.HelperName];
- var arguments = new Expression[]
- {
- Expression.Property(
- CompilationContext.BindingContext,
+ Expression.Property(
+ CompilationContext.BindingContext,
#if netstandard
- typeof(BindingContext).GetRuntimeProperty("TextWriter")),
+ typeof(BindingContext).GetRuntimeProperty("TextWriter")
#else
- typeof(BindingContext).GetProperty("TextWriter")),
+ typeof(BindingContext).GetProperty("TextWriter")
#endif
- Expression.Property(
- CompilationContext.BindingContext,
+ ),
+ Expression.Property(
+ CompilationContext.BindingContext,
#if netstandard
- typeof(BindingContext).GetRuntimeProperty("Value")),
+ typeof(BindingContext).GetRuntimeProperty("Value")
#else
- typeof(BindingContext).GetProperty("Value")),
+ typeof(BindingContext).GetProperty("Value")
#endif
- Expression.NewArrayInit(typeof(object), hex.Arguments.Select(a => Visit(a)))
- };
+ ),
+ Expression.Constant(hex.HelperName),
+ Expression.NewArrayInit(typeof(object), hex.Arguments.Select(a => Visit(a)))
+ };
+
+ if (CompilationContext.Configuration.Helpers.ContainsKey(hex.HelperName))
+ {
+ var helper = GetHelperWithName(CompilationContext.Configuration.Helpers[hex.HelperName]);
if (helper.Target != null)
{
return Expression.Call(
@@ -80,30 +84,43 @@ protected override Expression VisitHelperExpression(HelperExpression hex)
return Expression.Call(
Expression.Constant(this),
#if netstandard
- new Action>(LateBindHelperExpression).GetMethodInfo(),
+ new HandlebarsHelperWithName(InvokeLateBindHelper).GetMethodInfo(),
#else
- new Action>(LateBindHelperExpression).Method,
+ new HandlebarsHelperWithName(InvokeLateBindHelper).Method,
#endif
- CompilationContext.BindingContext,
- Expression.Constant(hex.HelperName),
- Expression.NewArrayInit(typeof(object), hex.Arguments));
+ arguments);
}
}
- private void LateBindHelperExpression(
- BindingContext context,
+ private HandlebarsHelperWithName GetHelperWithName(HandlebarsHelper helper)
+ => (writer, context, name, arguments) => helper(writer, context, arguments);
+
+ private void InvokeLateBindHelper(
+ TextWriter writer,
+ dynamic bindingContext,
+ string helperName,
+ IEnumerable