Table of Contents
Introduction to template engine
nodejs template engine summary and comparison
Home Web Front-end Front-end Q&A What is nodejs template engine

What is nodejs template engine

Nov 23, 2021 pm 02:20 PM
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.

What is nodejs template engine

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

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.

Is nodejs a backend framework? Is nodejs a backend framework? Apr 21, 2024 am 05:09 AM

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.

How to connect nodejs to mysql database How to connect nodejs to mysql database Apr 21, 2024 am 06:13 AM

To connect to a MySQL database, you need to follow these steps: Install the mysql2 driver. Use mysql2.createConnection() to create a connection object that contains the host address, port, username, password, and database name. Use connection.query() to perform queries. Finally use connection.end() to end the connection.

What are the global variables in nodejs What are the global variables in nodejs Apr 21, 2024 am 04:54 AM

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

What is the difference between npm and npm.cmd files in the nodejs installation directory? What is the difference between npm and npm.cmd files in the nodejs installation directory? Apr 21, 2024 am 05:18 AM

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.

Is there a big difference between nodejs and java? Is there a big difference between nodejs and java? Apr 21, 2024 am 06:12 AM

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.

Is nodejs a back-end development language? Is nodejs a back-end development language? Apr 21, 2024 am 05:09 AM

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.

Which one to choose between nodejs and java? Which one to choose between nodejs and java? Apr 21, 2024 am 04:40 AM

Node.js and Java each have their pros and cons in web development, and the choice depends on project requirements. Node.js excels in real-time applications, rapid development, and microservices architecture, while Java excels in enterprise-grade support, performance, and security.

See all articles