Does Handlebars require jQuery

2019-08-12 08:45发布

问题:

I'm trying to build a Handlebars app using only JavaScript. I've noticed in there documentation, they initialize using:

var source   = $("#entry-template").html();

Is this step required or is there a way around it using only native Javascript?

回答1:

No, it doesn't require jQuery.

Only reason why people prefer jQuery is because of ease of use with which jQuery perform various operations. It saves lots of time & additional effort.

Using handlebars with plain javscript will require some extra effort. If you have enough time then you should try out things with plain JavaScript.

Below given links might be of some help to understand things better -

http://code.tutsplus.com/tutorials/handlebarsjs-a-behind-the-scenes-look--net-32678

http://www.raymondcamden.com/2012/04/19/Demo-of-Handlebars-and-why-you-should-consider-a-templating-engine



回答2:

No. If you read the example further you would see they are only using jQuery to get the HTML content of an element. This is something that could have been done in vanilla JS. Any code that follows is not jQuery. The following is the same example sans jQuery. As you can see the way Handlebars is written is unaffected.

var source   = document.getElementById("entry-template").innerHTML;
var template = Handlebars.compile(source);