示例#1
0
 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"));
 }
示例#2
0
        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"));
        }