-
-
Save KevinJump/2d981be4ed51a0ce3f071636d6a11f60 to your computer and use it in GitHub Desktop.
ViewLocators experiments (Umbraco 9) - NOTE: THIS DOES NOT FULLY WORK.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
LayoutExpander - adds extra paths to where Asp.net Core looks for .cshtml files. | |
*/ | |
using Microsoft.AspNetCore.Mvc.Razor; | |
using Microsoft.Extensions.Options; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Aubergine.Core.Themes | |
{ | |
public class AubergineThemesRazorViewEngineOptionsSetup : IConfigureOptions<RazorViewEngineOptions> | |
{ | |
public void Configure(RazorViewEngineOptions options) | |
{ | |
if (options == null) | |
{ | |
throw new ArgumentNullException(nameof(options)); | |
} | |
options.ViewLocationExpanders.Add(new ThemeLayoutExpander()); | |
} | |
} | |
public class ThemeLayoutExpander : IViewLocationExpander | |
{ | |
private const string THEME_KEY = "theme"; | |
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations) | |
{ | |
if (context.Values.TryGetValue(THEME_KEY, out string theme)) | |
{ | |
string[] themeLocations = new[] { | |
$"/Themes/{theme}/{{0}}.cshtml", | |
$"/Views/Themes/{theme}/{{0}}.cshtml" | |
}; | |
viewLocations = themeLocations.Concat(viewLocations); | |
} | |
return viewLocations; | |
} | |
public void PopulateValues(ViewLocationExpanderContext context) | |
{ | |
// something clever (configy), to workout the theme for the site. | |
context.Values[THEME_KEY] = "Test"; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
IUmbracoBuilder Extension - so we can add themes to a site. | |
this only works if it the AddAubergineThemes is called before Umbraco's AddWebsite() call | |
suspect something inside that call is stopping the adding of new options to the razor views | |
*/ | |
using Microsoft.Extensions.DependencyInjection; | |
using Umbraco.Cms.Core.DependencyInjection; | |
namespace Aubergine.Core.Themes | |
{ | |
public static class ThemesBuilderExtension | |
{ | |
public static IUmbracoBuilder AddAubergineThemes(this IUmbracoBuilder builder) | |
{ | |
builder.Services.ConfigureOptions<AubergineThemesRazorViewEngineOptionsSetup>(); | |
return builder; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment