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.
Introduction to template engine
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.
nodejs template engine summary and comparison
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The main differences between Node.js and Tomcat are: Runtime: Node.js is based on JavaScript runtime, while Tomcat is a Java Servlet container. I/O model: Node.js uses an asynchronous non-blocking model, while Tomcat is synchronous blocking. Concurrency handling: Node.js handles concurrency through an event loop, while Tomcat uses a thread pool. Application scenarios: Node.js is suitable for real-time, data-intensive and high-concurrency applications, and Tomcat is suitable for traditional Java web applications.

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application
