Created
March 6, 2014 10:28
-
-
Save JohanObrink/9386961 to your computer and use it in GitHub Desktop.
Nustache magic
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 Nustache.Core; | |
using System; | |
using System.IO; | |
using System.Text; | |
public static class TemplateExtensions | |
{ | |
public static Template Load(this Template template, string html) | |
{ | |
using (var reader = new StringReader(html)) | |
{ | |
template.Load(reader); | |
} | |
return template; | |
} | |
public static string Render(this Template template, object data) | |
{ | |
var sb = new StringBuilder(); | |
using (var writer = new StringWriter(sb)) | |
{ | |
template.Render(data, writer, null); | |
} | |
return sb.ToString(); | |
} | |
} |
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 Nustache.Core; | |
using System; | |
using System.IO; | |
using System.Text; | |
using System.Web.Mvc; | |
namespace HtmlMail.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
// | |
// GET: /Home/ | |
public ActionResult Index() | |
{ | |
var html = "<!doctype html><html><head></head><body><h1>{{headline}}</h1>{{#msg}}<p>{{message}}</p>{{/msg}}</body></html>"; | |
var template = new Template().Load(html); | |
ViewBag.Html = template.Render(new { headline = "Hello World!", message = "Iz nize!", msg = true }); | |
return View(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment