With the increase of density of web front end development, AJAX and JSON are used more and more frequently, it's necessary to use many tags in front end development.
You may have a JSON object as below:
var data={
email: 'terry.li@gbin1.com,
gender: 'male'
}
Then you need to organize the JSON data into page elements.
var email, gender;
email= '
';
gender= '
';
$('#contentwrapper‘).append(content).append(gender);
The output is very simple:
If only simple codes are generated, then it's fine. However, if a page uses many these kinds of structures, it will bring much maintenance cost unless the page is maintained by the person who develops it.
To solve this problem, we usually introduce JavaScript template engine. As for template, if you have developed server side programs, for example J2EE related projects, you should be familiar with some template libraries such as Freemarker, sitemesh. Template libraries will help you organize and maintain code structure, they can improve code efficiency and reduce maintainenace cost as well.
There are many JavaScript template engines, the question now is how do we choose the right one.
In order to help you choose a template engine, here is a nice website : Template-engine-chooser.
In this website, we can see a list of frequenty used JS template engines such as mustache.js, microtemplate.js and jsrender.
There are also a list of questions about on the page. They are:
- Is this for use on the client or the server?
- How much logic should it have?
- Does it need to be one of the very fastest?
- Do you need to pre-compile templates?
- Do you need partials?
- Do you want a DOM structure, or just a string?
- Aside from template tags, should it be the same language before and after rendering?
If you are searching for JS template engine, you should have some questions in your mind, they may be the questions listed above. You only need to answer YES or NO to these questions, this tool will choose suitable template engine for you.
Of course, this is not the complete template engine list, but they include some famous templates, hope there wil be more templates added.
Source : 如何选择Javascript模æ¿å¼•æ“Žï¼ˆjavascript template engine)?