BorisMoore / jsrender

A lightweight, powerful and highly extensible templating engine. In the browser or on Node.js, with or without jQuery.
http://www.jsviews.com
MIT License
2.67k stars 339 forks source link

Provide support for passing an ID selector to $.templates() even when using JsRender without jQuery #363

Closed Spongman closed 3 years ago

Spongman commented 3 years ago

https://jsfiddle.net/gvLrdysz/1/

jsrender.templates("#foo").render({})

without jQuery, the above just returns "#foo"

Spongman commented 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({}));
BorisMoore commented 3 years ago

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! ####"

Spongman commented 3 years ago

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.

BorisMoore commented 3 years ago

Yes, it makes sense to add a note in the docs/readme, in order to make it clearer... I'll look at doing that....

BorisMoore commented 3 years ago

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!

BorisMoore commented 3 years ago

This has been resolved in v1.0.11. See https://www.jsviews.com/#jsr-quickstart@nojquery and https://www.jsviews.com/#compiletmpl@fromscriptblock...