Skip to content

Instantly share code, notes, and snippets.

@elfalem
Created August 8, 2019 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elfalem/10ee2705fc322baf311accb471432373 to your computer and use it in GitHub Desktop.
Save elfalem/10ee2705fc322baf311accb471432373 to your computer and use it in GitHub Desktop.
Stache View Engine
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
namespace foo.Stache{
public class StacheView : IView
{
public StacheView(string path){
Path = path;
}
public string Path {get; private set;}
public Task RenderAsync(ViewContext context)
{
var template = File.ReadAllText(Path);
var processedOutput = template.Replace("{{Message}}", context.ViewData["Message"]?.ToString());
return context.Writer.WriteAsync(processedOutput);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewEngines;
namespace foo.Stache
{
public class StacheViewEngine : IViewEngine
{
private const string ViewExtension = ".stache";
private string[] _viewLocationFormats = {
"Views/{1}/{0}" + ViewExtension,
"Views/Shared/{0}" + ViewExtension
};
public ViewEngineResult FindView(ActionContext context, string viewName, bool isMainPage)
{
if(context.ActionDescriptor.RouteValues.TryGetValue("controller", out var controllerName)){
var checkedLocations = new List<string>();
foreach(var locationFormat in _viewLocationFormats){
var possibleViewLocation = string.Format(locationFormat, viewName, controllerName);
if(File.Exists(possibleViewLocation)){
return ViewEngineResult.Found(viewName, new StacheView(possibleViewLocation));
}
checkedLocations.Add(possibleViewLocation);
}
return ViewEngineResult.NotFound(viewName, checkedLocations);
}
throw new Exception("Controller route value not found.");
}
public ViewEngineResult GetView(string executingFilePath, string viewPath, bool isMainPage)
{
if(string.IsNullOrEmpty(viewPath) || !viewPath.EndsWith(ViewExtension, StringComparison.OrdinalIgnoreCase)){
return ViewEngineResult.NotFound(viewPath, Enumerable.Empty<string>());
}
var appRelativePath = GetAbsolutePath(executingFilePath, viewPath);
if(File.Exists(appRelativePath)){
return ViewEngineResult.Found(viewPath, new StacheView(appRelativePath));
}
return ViewEngineResult.NotFound(viewPath, new List<string>{ appRelativePath});
}
private static string GetAbsolutePath(string executingFilePath, string viewPath){
if (IsAbsolutePath(viewPath))
{
// An absolute path already; no change required.
return viewPath.Replace("~/", string.Empty);
}
if(string.IsNullOrEmpty(executingFilePath)){
return $"/{viewPath}";
}
var index = executingFilePath.LastIndexOf('/');
return executingFilePath.Substring(0, index + 1) + viewPath;
}
private static bool IsAbsolutePath(string name) => name.StartsWith("~/") || name.StartsWith("/");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment