Last active
December 14, 2015 14:08
-
-
Save joeriks/5098153 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.WebPages; | |
namespace DynamicHtmlTemplates | |
{ | |
public class Templates : System.Dynamic.DynamicObject | |
{ | |
private System.Web.WebPages.WebPageRenderingBase page; | |
private string templatePath; | |
public Templates(string templatePath = "") | |
{ | |
this.page = System.Web.WebPages.WebPageContext.Current.Page; | |
var defaultPath = System.IO.Path.GetDirectoryName(this.page.TemplateInfo.VirtualPath); | |
if (templatePath == "") templatePath = defaultPath; | |
if (!templatePath.Contains("~")) templatePath = defaultPath + "/" + templatePath; | |
if (!templatePath.EndsWith("/")) templatePath += "/"; | |
this.templatePath = templatePath; | |
} | |
public dynamic AsDynamic() | |
{ | |
return this; | |
} | |
public System.Web.WebPages.HelperResult Display(string templateName, object[] args) | |
{ | |
if (templateName == "") | |
{ | |
templateName = args[0].GetType().Name; | |
} | |
return page.RenderPage(templatePath + templateName + ".cshtml", args); | |
} | |
public System.Web.WebPages.HelperResult DisplayWith(string templateName, Func<object, HelperResult> itemTemplate) | |
{ | |
return Display(templateName, new[] { itemTemplate }); | |
} | |
public System.Web.WebPages.HelperResult DisplayWith(string templateName, object viewModel, Func<object, HelperResult> itemTemplate) | |
{ | |
return Display(templateName, new[] { viewModel, itemTemplate }); | |
} | |
public System.Web.WebPages.HelperResult DisplayWith(string templateName, object viewModel, Func<object, HelperResult> itemTemplate, Func<object, HelperResult> alternativeItemTemplate) | |
{ | |
return Display(templateName, new[] { viewModel, itemTemplate, alternativeItemTemplate }); | |
} | |
public System.Web.WebPages.HelperResult Display(object value) | |
{ | |
var templateName = value.GetType().Name; | |
return Display(templateName, new[] { value }); | |
} | |
public static Func<object, HelperResult> ItemTemplate(Func<object, HelperResult> template) { return template; } | |
public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result) | |
{ | |
if (binder.Name != "Display") | |
{ | |
result = Display(binder.Name, args); | |
return true; | |
} | |
if (args.Length == 1) | |
{ | |
result = Display(args[0]); | |
return true; | |
} | |
if (args.Length > 1) | |
{ | |
result = Display((string)args[1], new[] { args[0] }); | |
return true; | |
} | |
result = "Invalid call"; | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment