Skip to content

Commit

Permalink
ViewEngine working!
Browse files Browse the repository at this point in the history
  • Loading branch information
JDemler committed Jul 25, 2017
1 parent 36cd83f commit 7192b23
Show file tree
Hide file tree
Showing 16 changed files with 249 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ private static Workspace CompileHandlebarsFiles(Project project, Workspace works
{
if (compilationResult?.Item2?.Any() ?? false)
{//Errors occured
if (compilationResult.Item2.OfType<HandlebarsTypeError>().Any(x => x.Kind == HandlebarsTypeErrorKind.UnknownPartial))
if (compilationResult.Item2.OfType<HandlebarsTypeError>().Any(x => x.Kind == HandlebarsTypeErrorKind.UnknownPartial ||
x.Kind == HandlebarsTypeErrorKind.UnknownLayout))
{//Unresolvable Partial... could be due to compiling sequence
foreach (var error in compilationResult.Item2) {
partialErrors.Add($"Compilation of '{name}' failed: {error.Message}");
Expand Down
16 changes: 11 additions & 5 deletions Compiler/Introspection/RoslynIntrospector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,25 @@ public INamedTypeSymbol GetTypeSymbol(string fullTypeName)
public bool RuntimeUtilsReferenced()
{
//TODO: Not only check if the type exists... but also if it is the correct version and supports everything needed from it
return projectCompilations[ContainingProject].GetTypeByMetadataName("CompiledHandlebars.RuntimeUtils.RenderHelper") != null;
if (projectCompilations[ContainingProject].GetTypeByMetadataName("CompiledHandlebars.RuntimeUtils.RenderHelper") != null)
{
return true;
}
//This is a workaround for a bug in roslyn. (https://github.com/dotnet/roslyn/issues/20939)
return projectCompilations[ContainingProject].ReferencedAssemblyNames.Any(x => x.Name == "CompiledHandlebars.RuntimeUtils");
}

public INamedTypeSymbol GetPartialHbsTemplate(string templateName)
{
return FindClassesWithNameAndAttribute(templateName, StringConstants.TEMPLATEATTRIBUTEFULL);
return FindClassesWithNameAndAttribute(templateName, StringConstants.TEMPLATEATTRIBUTEFULL, StringConstants.TEMPLATEATTRIBUTE);
}

public INamedTypeSymbol GetLayoutHbsTemplate(string layoutName)
{
return FindClassesWithNameAndAttribute(layoutName, StringConstants.LAYOUTATTRIBUTEFULL);
return FindClassesWithNameAndAttribute(layoutName, StringConstants.LAYOUTATTRIBUTEFULL, StringConstants.LAYOUTATTRIBUTE);
}

private INamedTypeSymbol FindClassesWithNameAndAttribute(string fullName, string attribute)
private INamedTypeSymbol FindClassesWithNameAndAttribute(string fullName, string attributeFull, string attribute)
{
var name = fullName.Split('.').Last();
foreach (var comp in projectCompilations.Values)
Expand All @@ -111,7 +116,8 @@ private INamedTypeSymbol FindClassesWithNameAndAttribute(string fullName, string
.OfType<INamedTypeSymbol>()
.Where(x => NamespaceUtility.IsPartOf(x.ToDisplayString(), fullName))
.FirstOrDefault(x => x.GetAttributes()
.Any(y => y.AttributeClass.Name.Equals(attribute)));
.Any(y => y.AttributeClass.Name.Equals(attributeFull) ||
y.AttributeClass.Name.Equals(attribute)));
if (template != null)
return template;
}
Expand Down
1 change: 1 addition & 0 deletions Examples/AspDotNetCore/AspDotNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Views\Partials\" />
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
Expand Down
8 changes: 5 additions & 3 deletions Examples/AspDotNetCore/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@ public IActionResult Index()
return View("~/Template.hbs", new ViewModel() { Name = "HansPeter" });
}

[Route("Greet/{name}")]
public IActionResult Greet(string name)
{
return View("Greet.hbs", new ViewModel() { Name = name });
{
return View("Greet", name);
}

[Route("Echo/{num}")]
public IActionResult echo(int num)
{
return View("Echo.hbs", num);
return View("Echo", num.ToString());
}
}
}
2 changes: 1 addition & 1 deletion Examples/AspDotNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void ConfigureServices(IServiceCollection services)
{
var ve_options = new CompiledHandlebarsViewEngineOptions()
{
ViewLocationFormats = new string[] { "~/Views/{1}/{0}", "~/Views/{0}", "~/{0}" }
ViewLocationFormats = new string[] { "~/Views/{1}/{0}.hbs", "~/Views/{0}.hbs", "~/{0}.hbs" }
};

services.AddMvc().AddViewOptions(options =>
Expand Down
35 changes: 33 additions & 2 deletions Examples/AspDotNetCore/Template.hbs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.Net;
using System.Text;
using System.Collections.Generic;
using CompiledHandlebars.RuntimeUtils;
using static CompiledHandlebars.RuntimeUtils.RenderHelper;

namespace AspDotNetCore
{
Expand All @@ -19,5 +17,38 @@ public static string Render(AspDotNetCore.ViewModel viewModel)
sb.Append("!</h1>\r\n</body>\r\n</html>");
return sb.ToString();
}

private static bool IsTruthy(bool b)
{
return b;
}

private static bool IsTruthy(string s)
{
return !string.IsNullOrEmpty(s);
}

private static bool IsTruthy(object o)
{
return o != null;
}

private static bool IsTruthy<T>(IEnumerable<T> ie)
{
return ie != null && ie.Any();
}

private static bool IsTruthy(int i)
{
return i != 0;
}

private class CompiledHandlebarsTemplateAttribute : Attribute
{
}

private class CompiledHandlebarsLayoutAttribute : Attribute
{
}
}
}
3 changes: 3 additions & 0 deletions Examples/AspDotNetCore/Views/Echo.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{{model System.String}}
{{layout Main}}
<h1>You said {{.}}</h1>
26 changes: 26 additions & 0 deletions Examples/AspDotNetCore/Views/Echo.hbs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Collections.Generic;
using AspDotNetCore.Views.Layouts;
using CompiledHandlebars.RuntimeUtils;
using static CompiledHandlebars.RuntimeUtils.RenderHelper;

namespace AspDotNetCore.Views
{
[CompiledHandlebarsTemplate]
public static class Echo
{
public static string Render(System.String viewModel)
{
var sb = new StringBuilder(64);
sb.Append(Main.PreRender(viewModel));
sb.Append("\r\n<h1>You said ");
sb.Append(WebUtility.HtmlEncode(viewModel));
sb.Append("</h1>");
sb.Append(Main.PostRender(viewModel));
return sb.ToString();
}
}
}
4 changes: 4 additions & 0 deletions Examples/AspDotNetCore/Views/Greet.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{model System.String}}
{{layout Main}}
<h1>Hello {{.}}</h1>
{{> Footer "Partial"}}
28 changes: 28 additions & 0 deletions Examples/AspDotNetCore/Views/Greet.hbs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Collections.Generic;
using AspDotNetCore.Views.Layouts;
using AspDotNetCore.Views.Partials;
using CompiledHandlebars.RuntimeUtils;
using static CompiledHandlebars.RuntimeUtils.RenderHelper;

namespace AspDotNetCore.Views
{
[CompiledHandlebarsTemplate]
public static class Greet
{
public static string Render(System.String viewModel)
{
var sb = new StringBuilder(64);
sb.Append(Main.PreRender(viewModel));
sb.Append("\r\n\t<h1>Hello ");
sb.Append(WebUtility.HtmlEncode(viewModel));
sb.Append("</h1>\r\n");
sb.Append(Footer.Render("Partial"));
sb.Append(Main.PostRender(viewModel));
return sb.ToString();
}
}
}
12 changes: 12 additions & 0 deletions Examples/AspDotNetCore/Views/Layouts/Main.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{model System.String}}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>{{.}}</title>
</head>
<body>
{{body}}
{{> Footer "Partial in the layout"}}
</body>
</html>
64 changes: 64 additions & 0 deletions Examples/AspDotNetCore/Views/Layouts/Main.hbs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Collections.Generic;
using AspDotNetCore.Views.Partials;

namespace AspDotNetCore.Views.Layouts
{
[CompiledHandlebarsLayout]
public static class Main
{
public static string PostRender(System.String viewModel)
{
var sb = new StringBuilder(64);
sb.Append("\r\n\t");
sb.Append(Footer.Render("Partial"));
sb.Append("\r\n</body>\r\n</html>");
return sb.ToString();
}

public static string PreRender(System.String viewModel)
{
var sb = new StringBuilder(64);
sb.Append("\r\n<!DOCTYPE html>\r\n<html>\r\n<head>\r\n <meta charset=\"utf-8\" />\r\n <title>");
sb.Append(WebUtility.HtmlEncode(viewModel));
sb.Append("</title>\r\n</head>\r\n<body>\r\n\t");
return sb.ToString();
}

private static bool IsTruthy(bool b)
{
return b;
}

private static bool IsTruthy(string s)
{
return !string.IsNullOrEmpty(s);
}

private static bool IsTruthy(object o)
{
return o != null;
}

private static bool IsTruthy<T>(IEnumerable<T> ie)
{
return ie != null && ie.Any();
}

private static bool IsTruthy(int i)
{
return i != 0;
}

private class CompiledHandlebarsTemplateAttribute : Attribute
{
}

private class CompiledHandlebarsLayoutAttribute : Attribute
{
}
}
}
2 changes: 2 additions & 0 deletions Examples/AspDotNetCore/Views/Partials/Footer.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{model System.String}}
<h2>This footer is embedded as {{.}}!</h2>
54 changes: 54 additions & 0 deletions Examples/AspDotNetCore/Views/Partials/Footer.hbs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Collections.Generic;

namespace AspDotNetCore.Views.Partials
{
[CompiledHandlebarsTemplate]
public static class Footer
{
public static string Render(System.String viewModel)
{
var sb = new StringBuilder(64);
sb.Append("\r\n<h2>This footer is embedded as ");
sb.Append(WebUtility.HtmlEncode(viewModel));
sb.Append("!</h2>");
return sb.ToString();
}

private static bool IsTruthy(bool b)
{
return b;
}

private static bool IsTruthy(string s)
{
return !string.IsNullOrEmpty(s);
}

private static bool IsTruthy(object o)
{
return o != null;
}

private static bool IsTruthy<T>(IEnumerable<T> ie)
{
return ie != null && ie.Any();
}

private static bool IsTruthy(int i)
{
return i != 0;
}

private class CompiledHandlebarsTemplateAttribute : Attribute
{
}

private class CompiledHandlebarsLayoutAttribute : Attribute
{
}
}
}
2 changes: 1 addition & 1 deletion RuntimeUtils/CompiledHandlebars.RuntimeUtils.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard1.6</TargetFramework>
<TargetFrameworks>net45;net46;netstandard1.6</TargetFrameworks>
<AssemblyName>CompiledHandlebars.RuntimeUtils</AssemblyName>
<RootNamespace>CompiledHandlebars.RuntimeUtils</RootNamespace>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions ViewEngine.Core/CompiledHandlebarsViewEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public ViewEngineResult FindView(ActionContext context, string viewName, bool is
var controllerName = RazorViewEngine.GetNormalizedRouteValue(context, ControllerKey);
var areaName = RazorViewEngine.GetNormalizedRouteValue(context, AreaKey);

var match = _options.PossibleVariants(viewName, controllerName, areaName).FirstOrDefault(x => _mappings.ContainsKey(x));
var match = _options.PossibleVariants(viewName, controllerName, areaName).FirstOrDefault(x => _mappings.ContainsKey(x.ToLower()));
if (!String.IsNullOrEmpty(match)) {
return ViewEngineResult.Found(viewName, new CompiledHandlebarsView(_mappings[match], match));
return ViewEngineResult.Found(viewName, new CompiledHandlebarsView(_mappings[match.ToLower()], match));
} else
{
return ViewEngineResult.NotFound(viewName, _options.PossibleVariants(viewName, controllerName, areaName));
Expand Down

0 comments on commit 7192b23

Please sign in to comment.