Last active
December 29, 2015 06:09
-
-
Save joeriks/7626979 to your computer and use it in GitHub Desktop.
Render nunjucks template with the help of ClearScript
This file contains hidden or 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 (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