Skip to content

Instantly share code, notes, and snippets.

@JohanObrink
Created March 6, 2014 10:28
Show Gist options
  • Save JohanObrink/9386961 to your computer and use it in GitHub Desktop.
Save JohanObrink/9386961 to your computer and use it in GitHub Desktop.
Nustache magic
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();
}
}
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