Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic support for layouts outside MVC. #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Postal.Tests/FileSystemRazorViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,33 @@ public void GivenViewFoundOnce_WhenFileChanged_ThenViewIsReloaded()
File.Delete(filename);
}
}

[Fact]
public void Layout_template_is_supported()
{
var tempPath = Path.GetTempPath();
var layoutFilename = Path.Combine(tempPath, "layout.cshtml");
var bodyFilename = Path.Combine(tempPath, "body.cshtml");
File.WriteAllText(layoutFilename, "layout-test\r\n@RenderBody()");
File.WriteAllText(bodyFilename, "@{Layout=\"layout.cshtml\";}\r\nbody-test");
try
{
var engine = new FileSystemRazorViewEngine(tempPath);
var view = engine.FindView(null, "body", null, true).View;
var context = new Mock<ViewContext>();
context.Setup(c => c.ViewData).Returns(new ViewDataDictionary(new object()));
using (var writer = new StringWriter())
{
view.Render(context.Object, writer);
var content = writer.GetStringBuilder().ToString();
Assert.Equal("layout-test\r\n\r\nbody-test", content);
}
}
finally
{
File.Delete(layoutFilename);
File.Delete(bodyFilename);
}
}
}
}
21 changes: 17 additions & 4 deletions src/Postal/FileSystemRazorView.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.IO;
using System.Web.Mvc;
using RazorEngine;
using RazorEngine.Templating;

namespace Postal
{
Expand All @@ -10,15 +10,28 @@ namespace Postal
/// </summary>
public class FileSystemRazorView : IView
{
static readonly ITemplateService DefaultRazorService = new TemplateService();

readonly ITemplateService razorService;
readonly string template;
readonly string cacheName;


/// <summary>
/// Creates a new <see cref="FileSystemRazorView"/> using the given view filename.
/// </summary>
/// <param name="filename">The filename of the view.</param>
public FileSystemRazorView(string filename) : this(DefaultRazorService, filename)
{
}

/// <summary>
/// Creates a new <see cref="FileSystemRazorView"/> using the given view filename.
/// </summary>
/// <param name="razorService">The RazorEngine ITemplateService to use to render the view</param>
/// <param name="filename">The filename of the view.</param>
public FileSystemRazorView(string filename)
public FileSystemRazorView(ITemplateService razorService, string filename)
{
this.razorService = razorService;
template = File.ReadAllText(filename);
cacheName = filename;
}
Expand All @@ -30,7 +43,7 @@ public FileSystemRazorView(string filename)
/// <param name="writer">The <see cref="TextWriter"/> used to write the rendered output.</param>
public void Render(ViewContext viewContext, TextWriter writer)
{
var content = Razor.Parse(template, viewContext.ViewData.Model, cacheName);
var content = razorService.Parse(template, viewContext.ViewData.Model, null, cacheName);

writer.Write(content);
writer.Flush();
Expand Down
57 changes: 41 additions & 16 deletions src/Postal/FileSystemRazorViewEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.IO;
using System.Linq;
using System.Web.Mvc;
using RazorEngine.Configuration;
using RazorEngine.Templating;

namespace Postal
{
Expand All @@ -13,6 +15,7 @@ namespace Postal
public class FileSystemRazorViewEngine : IViewEngine
{
readonly string viewPathRoot;
readonly ITemplateService razorService;

/// <summary>
/// Creates a new <see cref="FileSystemRazorViewEngine"/> that finds views within the given path.
Expand All @@ -21,43 +24,65 @@ public class FileSystemRazorViewEngine : IViewEngine
public FileSystemRazorViewEngine(string viewPathRoot)
{
this.viewPathRoot = viewPathRoot;

var razorConfig = new TemplateServiceConfiguration();
razorConfig.Resolver = new DelegateTemplateResolver(ResolveTemplate);
razorService = new TemplateService(razorConfig);
}

string GetViewFullPath(string path)
{
return Path.Combine(viewPathRoot, path);
}

/// <summary>
/// Tries to find a razor view (.cshtml or .vbhtml files).
/// </summary>
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
private string ResolveTemplate(string viewName)
{
var path = ResolveTemplatePath(viewName);
if (path == null) return null;
return File.ReadAllText(path);
}

private string ResolveTemplatePath(string viewName)
{
IEnumerable<string> searchedPaths;
var existingPath = ResolveTemplatePath(viewName, out searchedPaths);
return existingPath;
}

private string ResolveTemplatePath(string viewName, out IEnumerable<string> searchedPaths )
{
var possibleFilenames = new List<string>();

if (!partialViewName.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase)
&& !partialViewName.EndsWith(".vbhtml", StringComparison.OrdinalIgnoreCase))
if (!viewName.EndsWith(".cshtml", StringComparison.OrdinalIgnoreCase)
&& !viewName.EndsWith(".vbhtml", StringComparison.OrdinalIgnoreCase))
{
possibleFilenames.Add(partialViewName + ".cshtml");
possibleFilenames.Add(partialViewName + ".vbhtml");
possibleFilenames.Add(viewName + ".cshtml");
possibleFilenames.Add(viewName + ".vbhtml");
}
else
{
possibleFilenames.Add(partialViewName);
possibleFilenames.Add(viewName);
}

var possibleFullPaths = possibleFilenames.Select(GetViewFullPath).ToArray();

var existingPath = possibleFullPaths.FirstOrDefault(File.Exists);
searchedPaths = possibleFullPaths;
return existingPath;
}

/// <summary>
/// Tries to find a razor view (.cshtml or .vbhtml files).
/// </summary>
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
{
IEnumerable<string> searchedPaths;
var existingPath = ResolveTemplatePath(partialViewName, out searchedPaths);

if (existingPath != null)
{
return new ViewEngineResult(new FileSystemRazorView(existingPath), this);
}
else
{
return new ViewEngineResult(possibleFullPaths);
}
return new ViewEngineResult(new FileSystemRazorView(razorService, existingPath), this);

return new ViewEngineResult(searchedPaths);
}

/// <summary>
Expand Down