public static string Compile(string templateSource, string templateName) { J.JsContext scriptContext = new J.JsContext(); scriptContext.Load(Get()); scriptContext.SetCliValue("templateSource", templateSource.Replace("\r", "").Replace("\n", "")); scriptContext.SetCliValue("templateName", templateName); scriptContext.Run("var compiled = dust.compile(templateSource, templateName);"); return(scriptContext.GetValue <string>("compiled")); }
public static JsContext RunJavascript(this string javascriptSource, params CliProvider[] cliProviders) { JsContext ctx = new JsContext(); foreach (CliProvider o in cliProviders) { ctx.SetCliValue(o.VarName, o.Provider); } ctx.Run(javascriptSource); return(ctx); }
public static string Render(string compiled, string templateName, object data) { J.JsContext scriptContext = new J.JsContext(); scriptContext.Load(Get()); scriptContext.SetCliValue("compiled", Regex.Unescape(compiled)); scriptContext.SetCliValue("templateName", templateName); scriptContext.SetCliValue("jsonData", data.ToJson()); scriptContext.Run(@" var output; var error; dust.loadSource(compiled); dust.render(templateName, JSON.parse(jsonData), function(err, out){ error = err; output = out; });"); object error = scriptContext.GetValue <object>("error"); if (error != null) { throw new Exception("An error occurred rendering dust template: {0}"._Format(error.ToString())); } return(scriptContext.GetValue <string>("output")); }