I'm attempting to precompile JsRender templates from a class library written in C#, using the Jurassic script engine to execute JsRender.
Here is my code:
var engine = new Jurassic.ScriptEngine();
engine.Execute(JsRenderContents);
var precompiledTemplate = engine.CallGlobalFunction<string>(String.Concat("$.templates(\"", template, "\");"));
I've taken the JavaScript function call, $.templates()
, from this page which states that
$.templates(markupOrSelector) returns: Compiled template object
And my sample HTML template is simply
<li>{{:Name}}</li>
However, my code produces the exception:
'$.templates("<li>{{:Name}}</li>");' is not a function.
Now, I'm not 100% clear whether I can use the $ operator without jQuery being present. The author includes jQuery in several of his examples, but also states that jQuery is not required.
So what's going wrong? Is the documentation out of date for the version of JsRender taken from GitHub on the same day that I posted this question? (I'm aware that JsRender is still in beta.) Or maybe I'm misusing Jurassic?
EDIT:
I believe this is actually more a Jurassic question than a JsRender question. Specifically, I think this relates to Jurassic's global object, as JsRender is wrapped in an Immediately Invoked Function which passes this
, and I'm not certain than Jurassic provides this
.
It appears that I'm not the first to face this question. I've taken the advice from the last post on this page and changed my code to the 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 - probably because JsRender's IIF still passes this
instead of window
, and I don't want to modify the script.
Can anyone help push this forward? How can I call any JsRender function from Jurassic, given that Jurassic... I don't know... perhaps there's some notional difference in the way that Jurassic implements the global object.
I'm using jsRender + Jurassic to precompile my templates and generate js-files in T4. I spent a lot of time solving this problem and didn't find the answer, but read some articles, that helped.
See my code. It's working in my case. I'm sure I can help you to solve the issue, if this will not help:
jsRenderUtils.js contents (uneval function)
UPDATED: If you will render templates without jquery, you should add this: