Closed Spongman closed 3 years ago
as an aside: the templates
function seems weirdly overloaded. for example, when jQuery is loaded, the following returns nothing:
jsrender.templates("#### octothorps rule! ####").render({}));
With jQuery loaded, $.templates(markupOrSelector)
will first attempt treating the string markupOrSelector
as a jQuery selector. If the selector returns an element, it will use the content of the element as markup for the template. If the string is not a valid selector, or is a selector which does not match any element, it will return a template with markupOrSelector
as markup.
If jQuery is not loaded, then jQuery selectors are not used, and the string is treated immediately as markup.
See https://www.jsviews.com/#compiletmpl@fromscriptblock
By far the most common use case in the browser is with jQuery. But for advanced scenarios, or on the server, see https://www.jsviews.com/#nojqueryapi
Obviously in this case jQuery selectors don't apply. You need to use HTML APIs. as in https://www.jsviews.com/#download/pages-jsr
For your second point, with jQuery loaded,
jsrender.templates("#### octothorps rule! ####").render({});
will give an error that jsrender is not defined.
But with:
var html = $.templates("#### octothorps rule! ####").render({});
it will indeed give html = "#### octothorps rule! ####"
ok. you might want to clarify that in the readme on github, because it states that
The jsrender namespace provides the same methods/APIs as with jQuery, so if jQuery is not present you can still use all the API examples,
and further down it says
then, somewhere in your script:
var tmpl = $.templates("#myTemplate");
but nowhere that i can see does it mention that the code there requires jQuery.
Yes, it makes sense to add a note in the docs/readme, in order to make it clearer... I'll look at doing that....
On further thought, I agree with you that reading the docs may make people expect to be able to pass an ID selector for a script block to $.templates("#myTmpl")
even when jQuery is not loaded.
In addition, it is convenient, as an API.
So I am adding support for it, as a feature improvement, in the next update.
I will also update the docs to indicate that other jQuery selectors can be used too, but only if jQuery is loaded.
Thanks for calling this out!
This has been resolved in v1.0.11. See https://www.jsviews.com/#jsr-quickstart@nojquery and https://www.jsviews.com/#compiletmpl@fromscriptblock...
https://jsfiddle.net/gvLrdysz/1/
without jQuery, the above just returns
"#foo"