Roughing out starting point of multi-pass rendering for theme support

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4044044
This commit is contained in:
loudej
2009-12-15 00:04:32 +00:00
parent ad7b193d2c
commit 341bb208b0
5 changed files with 141 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Orchard.Mvc.ViewEngines;
namespace Orchard.Mvc.Html {
public static class LayoutHelperExtensions {
public static void RenderBody(this HtmlHelper html) {
var layoutContext = OrchardLayoutContext.From(html.ViewContext);
html.ViewContext.HttpContext.Response.Output.Write(layoutContext.BodyContent);
}
}
}

View File

@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web.Mvc;
namespace Orchard.Mvc.ViewEngines {
public class LayoutView : IView {
private IView[] _views;
public LayoutView(IEnumerable<IView> views) {
_views = views.ToArray();
}
public void Render(ViewContext viewContext, TextWriter writer) {
var orchardViewContext = OrchardLayoutContext.From(viewContext);
for (var index = 0; index != _views.Length; ++index)
{
var view = _views[index];
if (index == _views.Length - 1) {
view.Render(viewContext, writer);
}
else {
//TEMP: to be replaced with an efficient spooling writer
var childWriter = new StringWriter();
view.Render(viewContext, childWriter);
orchardViewContext.BodyContent = childWriter.ToString();
}
}
}
}
}

View File

@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace Orchard.Mvc.ViewEngines {
public class LayoutViewEngine : IViewEngine {
private readonly ViewEngineCollection _viewEngines;
public LayoutViewEngine(ViewEngineCollection viewEngines) {
_viewEngines = viewEngines;
}
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) {
return new ViewEngineResult(Enumerable.Empty<string>());
}
public ViewEngineResult FindView(
ControllerContext controllerContext,
string viewName,
string masterName,
bool useCache) {
// TODO: is there an optimization around useCache for
// this implementation? since IView can't re-execute, maybe not...
// if action returned a View with explicit master -
// this will bypass the multi-pass layout strategy
if (!string.IsNullOrEmpty(masterName))
return new ViewEngineResult(Enumerable.Empty<string>());
var bodyView = _viewEngines.FindPartialView(controllerContext, viewName);
var layoutView = _viewEngines.FindPartialView(controllerContext, "layout");
var documentView = _viewEngines.FindPartialView(controllerContext, "document");
if (bodyView.View == null ||
layoutView.View == null ||
documentView.View == null) {
return new ViewEngineResult(
(bodyView.SearchedLocations ?? Enumerable.Empty<string>())
.Concat((layoutView.SearchedLocations ?? Enumerable.Empty<string>()))
.Concat((documentView.SearchedLocations ?? Enumerable.Empty<string>()))
);
}
var view = new LayoutView(new[] {
bodyView.View,
layoutView.View,
documentView.View,
});
return new ViewEngineResult(view, this);
}
public void ReleaseView(ControllerContext controllerContext, IView view) {
}
}
}

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
namespace Orchard.Mvc.ViewEngines {
public class OrchardLayoutContext {
private static readonly object _key = typeof(OrchardLayoutContext);
public string BodyContent { get; set; }
public static OrchardLayoutContext From(ControllerContext context) {
return From(context.HttpContext);
}
public static OrchardLayoutContext From(HttpContextBase context) {
if (context.Items.Contains(_key)) {
context.Items.Add(_key, new OrchardLayoutContext());
}
return (OrchardLayoutContext)context.Items[_key];
}
}
}

View File

@@ -200,6 +200,7 @@
<Compile Include="Mvc\Html\ContentItemExtensions.cs" />
<Compile Include="Mvc\Html\ItemDisplayExtensions.cs" />
<Compile Include="Mvc\Html\ItemEditorExtensions.cs" />
<Compile Include="Mvc\Html\LayoutHelperExtensions.cs" />
<Compile Include="Mvc\Html\MvcFormAntiForgeryPost.cs" />
<Compile Include="Mvc\MvcModule.cs" />
<Compile Include="Mvc\Html\HtmlHelperExtensions.cs" />
@@ -213,6 +214,9 @@
<Compile Include="Mvc\Results\NotFoundResult.cs" />
<Compile Include="Mvc\OrchardControllerIdentificationStrategy.cs" />
<Compile Include="Mvc\Routes\RouteExtensions.cs" />
<Compile Include="Mvc\ViewEngines\LayoutView.cs" />
<Compile Include="Mvc\ViewEngines\LayoutViewEngine.cs" />
<Compile Include="Mvc\ViewEngines\OrchardLayoutContext.cs" />
<Compile Include="Mvc\ViewModels\AdminViewModel.cs" />
<Compile Include="Mvc\ViewModels\BaseViewModel.cs" />
<Compile Include="Mvc\ViewPage.cs">