javascript - Using Jurassic to precompile a JsRender template server-side -
i'm attempting precompile jsrender templates class library written in c#, using jurassic script engine execute jsrender.
here code:
var engine = new jurassic.scriptengine(); engine.execute(jsrendercontents); var precompiledtemplate = engine.callglobalfunction<string>(string.concat("$.templates(\"", template, "\");"));
i've taken javascript function call, $.templates()
, this page states that
$.templates(markuporselector) returns: compiled template object
and sample html template simply
<li>{{:name}}</li>
however, code produces exception:
'$.templates("<li>{{:name}}</li>");' not function.
now, i'm not 100% clear whether can use $ operator without jquery being present. author includes jquery in several of examples, states jquery not required.
so what's going wrong? documentation out of date version of jsrender taken github on same day posted question? (i'm aware jsrender still in beta.) or maybe i'm misusing jurassic?
edit:
i believe more jurassic question jsrender question. specifically, think relates jurassic's global object, jsrender wrapped in invoked function passes this
, , i'm not jurassic provides this
.
it appears i'm not first face question. i've taken advice last post on this page , changed code following:
var engine = new jurassic.scriptengine(); engine.execute(jsrendercontents); engine.global["window"] = engine.global; var precompiledtemplate = engine.callglobalfunction<string>(string.concat("window.jsviews.templates(\"", template, "\");"));
which didn't work - because jsrender's iif still passes this
instead of window
, , don't want modify script.
can push forward? how can call any jsrender function jurassic, given jurassic... don't know... perhaps there's notional difference in way jurassic implements global object.
i'm using jsrender + jurassic precompile templates , generate js-files in t4. spent lot of time solving problem , didn't find answer, read articles, helped.
see code. it's working in case. i'm sure can solve issue, if not help:
var engine = new jurassic.scriptengine(); var jsrenderpath = "/pathtodir/jsrender.js"; var jsunevalpath = "/pathtodir/jsrenderutils.js"; engine.executefile(jsrenderpath); engine.executefile(jsunevalpath); engine.evaluate("function rendertemplate(name, markup) { var tmpl = this.jsviews.templates(name, markup); return uneval(tmpl); }"); var compiledtemplatestring = engine.callglobalfunction<string>("rendertemplate", templatename, templatestring); var result = "$.templates['" + templatename + "'] = " + compiledtemplatestring + ";";
jsrenderutils.js contents (uneval function)
function uneval(obj, known) { var root = (known === undefined), result; known = known || []; // values fail eval() if not wrapped in ( ) parenthesises var wraproot = function (result) { return root ? ("(" + result + ")") : result; }; // special objects if (obj === null) return "null"; if (obj === undefined) return "undefined"; if (obj !== obj) // isnan type coercion, can't use that. return "nan"; if (obj === infinity) return "infinity"; if (obj === -infinity) return "-infinity"; // atoms switch (typeof obj) { case 'function': return wraproot(obj.tostring()); case 'number': case 'boolean': return obj.tostring(); case 'string': return "\"" + obj.tostring() + "\""; } // circular reference check non-atoms if (known.indexof(obj) !== -1) return "null";//throw new error("circular references detected while unevaling."); known.push(obj); // specialized types if (obj instanceof array) return "[" + obj.map(function (o) { return uneval(o, known); }).join(",") + "]"; if (obj instanceof date) return wraproot("new date('" + obj.tostring() + "')"); // hashes var key, pairs = []; (key in obj) { var val; switch (key) { case "render": val = "$.fn.render"; break; case "markup": val = "null"; break; default: val = uneval(obj[key], known); } pairs.push("\r\n" + key + " : " + val); } return wraproot("{" + pairs.join(",") + "}"); };
updated: if render templates without jquery, should add this:
$ = window.jsviews; $.fn = { render: function (data, view, j, u) { return this.fn(data, view, j, u); } };
Comments
Post a Comment