Skip to content

Instantly share code, notes, and snippets.

@barryokane
Created March 11, 2017 04:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barryokane/71c0f64cbbf2c7c14336c0051224ce17 to your computer and use it in GitHub Desktop.
Save barryokane/71c0f64cbbf2c7c14336c0051224ce17 to your computer and use it in GitHub Desktop.
Inspired by Heather Floyd's article (http://24days.in/umbraco-cms/2016/unique-sites-using-theming), however our use case is slightly different: we want to share a library of partials between multiple Umbraco sites with custom overrides on specific sites
using System.Linq;
using Umbraco.Web.Mvc;
namespace Endzone.Umbraco.PatternLib
{
public class PatternLibRazorViewEngine : RenderViewEngine
{
public PatternLibRazorViewEngine() : base()
{
/*
* Default partials "patterns" folder, these can be overridden simply by placing a partial with the same name in "Views/Partials"
*/
const string patternLibViewFolder = "~/Views/_patterns";
/*
* Let Umbraco's RenderViewEngine do the work, just add our partials view location to the list...
*/
var umbracoPartialViewlocations = PartialViewLocationFormats.ToList();
var patternLibPartialViewLocations = umbracoPartialViewlocations.Select(x => x.Replace("~/Views", patternLibViewFolder) ).ToArray();
umbracoPartialViewlocations.AddRange(patternLibPartialViewLocations);
PartialViewLocationFormats = umbracoPartialViewlocations.ToArray();
}
}
}
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Web.Mvc;
namespace Endzone.Umbraco.PatternLib
{
/// <summary>
/// Registers site specific Umbraco application event handlers
/// </summary>
public class UmbracoEvents : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarting(umbracoApplication, applicationContext);
/*
* Replace the Umbraco RenderViewEngine with our PatternLibRazorViewEngine
* this allows us to add our own View locations without too many extra steps
*/
IViewEngine remove = null;
ViewEngines.Engines.ForEach(x => {
if (x.GetType() == typeof(RenderViewEngine))
remove = x;
});
if (remove != null)
ViewEngines.Engines.Remove(remove);
ViewEngines.Engines.Add(new PatternLibRazorViewEngine());
}
}
}
@smdooley
Copy link

smdooley commented Apr 6, 2020

Hi Barry

Have you managed to use this approach on any live projects?

@barryokane
Copy link
Author

@smdooley : not verbatum. We have a similar concept in a base project. Are you looking for a solution to this?

@smdooley
Copy link

smdooley commented Apr 7, 2020

@smdooley : not verbatum. We have a similar concept in a base project. Are you looking for a solution to this?

I have used http://24days.in/umbraco-cms/2016/unique-sites-using-theming for a few projects but I have experienced the occasional issue with third-party App_Plugins and routing, so I am interested to see/hear other approaches.

Is your approach compatible with the Grid Layout and Umbraco Forms?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment