Skip to content

Commit f24a0fd

Browse files
committed
Fixed helper method bug
1 parent cb14516 commit f24a0fd

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

Diff for: Compiler/Introspection/RoslynIntrospector.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ public INamedTypeSymbol GetLayoutHbsTemplate(string layoutName)
107107
return FindClassesWithNameAndAttribute(layoutName, StringConstants.LAYOUTATTRIBUTEFULL, StringConstants.LAYOUTATTRIBUTE);
108108
}
109109

110+
/// <summary>
111+
/// Finds a class with a certain name and attribute
112+
/// Used to find layouts and partial templates
113+
///
114+
/// Alas we have to check against both the full and the nonfull attribute name because of inconsistencies in Roslyn
115+
/// when working with asp.net core projects
116+
/// </summary>
117+
/// <param name="fullName"></param>
118+
/// <param name="attributeFull"></param>
119+
/// <param name="attribute"></param>
120+
/// <returns></returns>
110121
private INamedTypeSymbol FindClassesWithNameAndAttribute(string fullName, string attributeFull, string attribute)
111122
{
112123
var name = fullName.Split('.').Last();
@@ -142,7 +153,11 @@ public IMethodSymbol GetHelperMethod(string funtionName, List<ITypeSymbol> param
142153
var candidates = comp.GetSymbolsWithName(x => x.Equals(funtionName))
143154
.OfType<IMethodSymbol>()
144155
.Where(x => x.IsStatic &&
145-
x.GetAttributes().Any(y => y.AttributeClass.Name.Equals(StringConstants.HELPERMETHODATTRIBUTEFULL)));
156+
// The check for both HelperMethodAttribute and HelperMethodAttributeFull because when loading a asp.net core project
157+
// we the attribute name is HelpermethodAttribute while when loading a .net framework project the attribute name is
158+
// HelperMethodAttribute
159+
x.GetAttributes().Any(y => y.AttributeClass.Name.Equals(StringConstants.HELPERMETHODATTRIBUTEFULL)
160+
|| y.AttributeClass.Name.Equals(StringConstants.HELPERMETHODATTRIBUTE)));
146161
var helperMethod = candidates.FirstOrDefault(x => DoParametersMatch(x, parameters));
147162
if (helperMethod != null)
148163
return helperMethod;

Diff for: Examples/AspDotNetCore/Startup.cs

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Extensions.Logging;
1010
using CompiledHandlebars.ViewEngine.Core;
1111
using System.Reflection;
12+
using CompiledHandlebars.RuntimeUtils;
1213

1314
namespace AspDotNetCore
1415
{
@@ -47,5 +48,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
4748
template: "{controller=Test}/{action=Index}/{id?}");
4849
});
4950
}
51+
52+
[CompiledHandlebarsHelperMethod]
53+
public static string TestHelper(ViewModel vm)
54+
{
55+
return $"ViewModel(Name:{vm.Name})";
56+
}
5057
}
5158
}

Diff for: Examples/AspDotNetCore/Template.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
<html>
33
<body>
44
<h1>Hello {{Name}}!</h1>
5+
<p>{{TestHelper .}}</p>
56
</body>
67
</html>

Diff for: Examples/AspDotNetCore/Template.hbs.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ public static string Render(AspDotNetCore.ViewModel viewModel)
1616
var sb = new StringBuilder(64);
1717
sb.Append("\r\n<html>\r\n<body>\r\n\t<h1>Hello ");
1818
sb.Append(WebUtility.HtmlEncode(viewModel.Name));
19-
sb.Append("!</h1>\r\n</body>\r\n</html>");
19+
sb.Append("!</h1>\r\n\t<p>");
20+
sb.Append(WebUtility.HtmlEncode(Startup.TestHelper(viewModel)));
21+
sb.Append("</p>\r\n</body>\r\n</html>");
2022
return sb.ToString();
2123
}
2224
}

0 commit comments

Comments
 (0)