Skip to content

Instantly share code, notes, and snippets.

@joeriks
Last active December 29, 2015 06:09
Show Gist options
  • Select an option

  • Save joeriks/7626979 to your computer and use it in GitHub Desktop.

Select an option

Save joeriks/7626979 to your computer and use it in GitHub Desktop.
Render nunjucks template with the help of ClearScript
using (var engine = new V8ScriptEngine())
{
engine.Execute("var window = {}"); // easiest way I found to keep the nunjucks reference somewhere
using (var client = new WebClient())
{
// get the nunjucks js source
var nunjucks = client.DownloadString("http://jlongster.github.io/nunjucks/files/nunjucks.js");
// run it
engine.Evaluate(nunjucks);
}
// use the Script method that dynamically hosts the js objects (rather slow ~200ms on my machine):
var renderIt1 = engine.Script.window.nunjucks.renderString("hello {{username}}", new { username = "James" });
// use a plain evaluate method for best performance (fast ~1 ms on my machine):
var renderIt2 = engine.Evaluate("window.nunjucks.renderString('hello {{username}}', { username : 'James' })");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment