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")); }
/// <summary> /// Get a json string by reading an object from a javascript file /// </summary> /// <param name="jsLiteralFile"></param> /// <param name="objName"></param> /// <returns></returns> public static string JsonFromJsLiteralFile(this FileInfo jsLiteralFile, string objName) { string json = Bam.Net.Javascript.ResourceScripts.Get("json2.js"); string database = File.ReadAllText(jsLiteralFile.FullName); string command = string.Format("\r\n;var objJson = JSON.stringify({0});", objName); string script = "{0}{1}{2}"._Format(json, database, command); JsContext context = script.RunJavascript(); string result = context.GetValue <string>("objJson"); return(result); }
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")); }