Skip to content

Instantly share code, notes, and snippets.

@JohanObrink
Created March 6, 2014 10:28
Show Gist options
  • Select an option

  • Save JohanObrink/9386961 to your computer and use it in GitHub Desktop.

Select an option

Save JohanObrink/9386961 to your computer and use it in GitHub Desktop.

Revisions

  1. JohanObrink created this gist Mar 6, 2014.
    26 changes: 26 additions & 0 deletions Extensions
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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();
    }
    }
    24 changes: 24 additions & 0 deletions usage
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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();
    }

    }
    }