Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of nodejs template engine

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "nodejs template engine what is the role of", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "what is the role of nodejs template engine" bar!

Nodejs template engine refers to the rendering template, you can use template files to generate HTML files dynamically, and you can integrate the data in the application into HTML files according to certain rules. Common node template engines are: Mustache, Dust.js, doT, Jade, EJS, swig and so on.

This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.

Introduction of template engine

In a web application, if you only use server-side code to write client-side html code, the front and back ends are not separated, then it will cause a lot of work, and the written code will be more difficult to read and maintain. If you only use the client-side static HTML file, then the back-end logic will be more difficult to integrate into the client-side HTML code.

In order to facilitate maintenance, and make the back-end logic better integrate into the front-end HTML code, and at the same time facilitate maintenance, many third-party developers have developed a variety of Nodejs template engines.

So what is a template engine, we can understand it separately.

Template: a mold (structure). For you to set up data and follow different logic according to different data.

Engine: a processor (compile, run) that finally renders the HTML code

So, together to understand the template engine: use template files to generate HTML files dynamically, and then integrate the data in the application into HTML files according to certain rules. Similar to the background jsp language.

In a nutshell, a template engine is a rendering template.

Summary and comparison of nodejs template engine

Mustache

See the official website: http://mustache.github.io/

Mustache is a very easy-to-use template engine, known as an illogical template engine, which can be used in HTML, configuration files, source code, and so on.

It is called an illogical template engine because it has no structures such as if statements, else conditions, for loops, and so on. Only the label, replace the label with a value, the value can be a hash or an object, as simple as that.

There are two definitions of Mustache templates, 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, Cellular languages, Go, Lua, etc.

Mustache can also be well integrated with editors such as 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 released its first version at the end of 2009. The first version of the template engine is implemented in the Ruby language, running YAML template text. The main principles adopted are: one is to emphasize "no logic", there is no explicit control flow statement, and all controls are data-driven; the other is to emphasize the "separation of logic and representation", so it is impossible to embed application logic into the template.

Handlebars

Official website:

< http://handlebarsjs.com/>

Handlebars.js is an extension of the Mustache template language created by Chris Wanstrath. Both Handlebars.js and Mustache are illogical template languages that keep the view and code separate.

Generally speaking, the syntax of Handlebars.js templates is a superset of Mustache templates.

The basic syntax can be found in the help page of Mustache:

< http://mustache.github.com/mustache.5.html>

Handlebars allows templates to be precompiled and included in JavaScript code, making startup time shorter.

Several points that Handlebars is not compatible with Mustache:

* Handlebars does not perform recursive lookups by default unless the compat flag must be set to enable this feature at compile time. Users should be aware that enabling this flag will incur performance costs.

* optional Mustache-style Lambdas expressions are not supported.

* alternate delimiters are not supported

Dust.js

Official website:

< http://www.dustjs.com/>

Dust is a Javascript template engine that inherits the ctemplatelanguage style and is designed to run asynchronously on servers and browsers.

Compared with other template engines, Dust is not illogical, but has less logic.

* you cannot write any Javascript in a Dust template. However, you still have basic logical operators, such as comparison, less / greater, exist / do not exist. This strikes a balance between template readability and data control.

* Dust encourages moving logic to the data model. You can create a function in the model and then call it through the template, giving you complete control over how the template is rendered without confusing the logic.

* Asynchronous template loading, rendering and streaming. Therefore, it is not necessary to preload the template.

* templates can be combined, and partial inclusion and dynamic template blocks are supported. The templates are spliced together to build the layout manually.

* HTML is secure and format-independent. Dust protects against cross-site scripting attacks by securely escaping data.

* High performance. Strike a balance between performance and functionality. Although it is not as fast as Mustache, its asynchronism means that 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 series of useful functional programming helpers without extending any built-in objects.

Underscore provides more than 100 functions, supports map, filter, invoke and other features, as well as function binding, JavaScript templates, fast index creation, deep equivalence testing, and so on.

DoT

Official website:

< http://olado.github.io/doT/>

DoT is not the easiest template engine to use, but it meets the following requirements:

* if the template engine needs to be used on both the client and server side

* if the template requires a lot of logic and wants the application to run very fast

* if a precompiled template is required

Jade

Official website:

< http://jade-lang.com/>

Using this template engine, you can write less code and make development easier. However, it is time-consuming to use it in a Node.js environment, because you have to convert the file to HTML and then to Jade.

EJS

Official website:

< http://ejs.co/>

EJS is the default template language for CanJS and provides the use of real-time binding with Observes. EJS is very easy to use, just write the desired HTML in the template, as well as some magic tags that represent dynamic behavior. JES does not support the block feature.

Swig

Swig does not have an abstract HTML syntax, but you can populate the syntax of Angular.js with Swig to support block functionality.

Thank you for your reading, the above is the content of "what is the role of nodejs template engine", after the study of this article, I believe you have a deeper understanding of what the role of nodejs template engine is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report