日期:2014-05-16  浏览次数:20338 次

Top 5 JS template

Progressions in application development with JavaScript has led to a number of libraries existing to support them. Developer Jack Franklin talks through some of the popular templating libraries.

When you build a JavaScript application, you'll almost certainly use some JavaScript templates. Rather than use a library like jQuery (or vanilla JavaScript) to update your HTML when values update, you can use templates, which cleans up your code hugely. In this article, we'll look at some popular templating libraries.

01.?Mustache

JavaScript templatesMustache is often considered the base for JavaScript templating

Mustache?is often considered the base for JavaScript templating. Another popular solution, Handlebars, actually builds on top of Mustache, but that doesn't mean that it isn't a very good templating solution. Here's an example:

?Mustache.render("Hello, {{name}}", { name: "Jack" });
???
?// returns: Hello, Jack

Once Mustache is included on your page, you have access to the global 'Mustache' object. The main method you'll use is 'render', which takes two arguments. The first is the actual template, and the second is any arguments that need to be passed to it.

In the above example, you can see I've referenced '{{name}}'. Two braces around something is the Mustache syntax to show that it's a placeholder. When Mustache compiles it, it will look for the 'name' property in t