Home > Article > Web Front-end > What is nodejs template engine
The nodejs template engine refers to rendering templates. You can use template files to dynamically generate HTML files. During generation, you can integrate data from the application into HTML files according to certain rules. Common node template engines include: Mustache, Dust.js, doT, Jade, EJS, swig, etc.
The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.
In a web application, if you only use server-side code to write client-side html code, and the front-end and back-end are not separated, it will cause A lot of work, and the code written will be difficult to read and maintain. If you only use the client's static HTML files, it will be more difficult for the back-end logic to be integrated into the client's HTML code.
In order to facilitate maintenance, enable the back-end logic to be better integrated into the front-end HTML code, and facilitate maintenance, many third-party developers have developed various Nodejs template engines.
So what is a template engine, we can understand it separately.
Template: a model (structure). Allows you to set data and implement different logic based on different data
Engine: a processor (compile, run), and finally render the HTML code
So, let’s understand the template engine together: Use template files to dynamically generate HTML files. During generation, you can integrate data from the application into the HTML files according to certain rules. Similar to the background jsp language.
Simply put, the template engine is to render templates.
Mustache
See the official website: http://mustache.github.io/
Mustache is a very simple and easy-to-use template engine. It is known as a logic-free template engine and can be used in HTML, configuration files, source code and other scenarios.
The reason why it is called a template engine without logic is that it does not have if statements, else conditions, for loops and other structures. Just tags, replace tags with values, values can be hashes or objects, it's that simple.
Mustache templates have two definitions, Mustache (1) and Mustache (5)
Mustache supports mainstream programming languages, such as Ruby, JavaScript, Python, Erlang, node.js, PHP, Perl, Perl6, Objective-C, Java, Android, C, Go, Lua, etc.
Mustache can also be well integrated with editors TextMate, Vim, Emacs, Coda, Atom, etc.
Mustache (1) Manual: < http://mustache.github.io/mustache.5.html>
Mustache (5) Manual: < http://mustache.github.io /mustache.1.html>
Mustache (1) was inspired by ctemplate and the first version was released at the end of 2009. The first version of the template engine was implemented in Ruby and ran YAML template text. The main principles adopted are: first, emphasizing "no logic", there are no explicit control flow statements, all controls are driven by data; second, emphasizing "separation of logic and presentation", it is impossible to embed application logic into templates .
Handlebars
Official website: < http://handlebarsjs.com/>
Handlebars.js is the Mustache template language created by Chris Wanstrath extension. Both Handlebars.js and Mustache are logic-free template languages that keep views and code separated.
Generally speaking, the syntax of Handlebars.js template is a superset of Mustache template.
For its basic syntax, please refer to the Mustache help page: < http://mustache.github.com/mustache.5.html>
Handlebars allow templates to be precompiled and included in JavaScript code, making startup time shorter.
Some points why Handlebars is not compatible with Mustache:
* Handlebars does not perform recursive search by default, unless the compat flag must be set to enable this feature at compile time. Users should be aware that there is a performance cost to enabling this flag.
* Optional Mustache-style Lambdas expressions are not supported.
* Alternative delimiters are not supported
Dust.js
Official website: < http://www.dustjs.com/>
Dust is a Javascript template engine that inherits the ctemplate language style and is designed to run asynchronously on the server and browser.
Compared with other template engines, Dust is not illogical, it just has less logic.
*You cannot write arbitrary Javascript in Dust templates. However, you still have basic logical operators like comparison, less than/greater than, presence/absence. This strikes a balance between template readability and data control.
* Dust encourages moving logic to the data model. Functions can be created in the model and then called from the template, giving you complete control over how the template is rendered without cluttering the logic.
*Asynchronous template loading, rendering and streaming. So there is no need to preload the template.
*Combinable templates support partial inclusion and dynamic template blocks, splicing templates together to achieve manual construction of layouts.
* HTML is safe and format-independent. Dust prevents cross-site scripting attacks by safely escaping data.
*high performance. Strike a balance between performance and functionality. While it's not as fast as Mustache, its asynchronous nature means large templates can be rendered faster.
* Dust works in JavaScript.
Underscore.js
Official website: < http://underscorejs.org/>
Underscore is a JavaScript library that provides a A collection of useful functional programming helpers without extending any built-in objects.
Underscore provides more than 100 functions, supporting map, filter, invoke and other features, as well as function binding, JavaScript templates, fast index creation, deep equivalence testing, etc.
doT
Official website: < http://olado.github.io/doT/>
doT is not the easiest template to use engine, but it meets the following needs:
* If the template engine needs to be used on both the client and server sides
* If the template requires huge logic and you want the application to run quickly Quick
* If you need precompiled template
Jade
Official website: < http://jade-lang.com/>
Using this template engine allows you to write less code and make development easy. However, it is more time-consuming to use it in the Node.js environment because the file must be converted to HTML first and then converted to Jade.
EJS
Official website: < http://ejs.co/>
EJS is the default template language of CanJS. It provides the use of real-time bindings with Observes. EJS is very easy to use, just write the HTML you want in the template, and some magic tags that represent dynamic behavior. JES does not support the block function.
swig
Swig does not have abstract HTML syntax, but you can use Swig to fill in the syntax of Angular.js and support the block function.
For more node-related knowledge, please visit: nodejs tutorial! !
The above is the detailed content of What is nodejs template engine. For more information, please follow other related articles on the PHP Chinese website!