search
HomeWeb Front-endJS TutorialHow to use node front-end template engine Jade tag

This time I will show you how to use the node front-endtemplate engineJade tag, and use nodefront-end templateengine Jade tagWhat are the precautionsWhat are the following? This is a practical case, let’s take a look at it.

1. Document declaration

#When we start writing an html page, we must first write the DOCTYPE document declaration. Now it is usually the case Next we all use the HTML5 document declaration method, so how should we write it in jade?

There are two ways to write document declarations in jade:

  1. We can write doctype html directly in the jade file

  2. jade provides us with a simple writing method, (but it seems that jade does not recommend this method in the new version after the upgrade -_-||| )

Of course , jade also supports other types of document declarations by default, just use doctype to follow the following options. By default, jade supports:

var doctypes = exports.doctypes = {
 '5': 'nbsp;html>',
 'xml': '<?xml  version="1.0" encoding="utf-8" ?>',
 'default': 'nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
 'transitional': 'nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
 'strict': 'nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
 'frameset': 'nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
 '1.1': 'nbsp;html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
 'basic': 'nbsp;html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">',
 'mobile': 'nbsp;html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">'
};

doctype is not case-sensitive, so the following two have the same effect:

doctype Default
doctype default

For example: if we want to write an XHTML 1.0 Strict document declaration , you can write like this:

doctype strict

The compilation result is as follows:

Copy code The code is as follows:

##2、 Tag

The tag in jade is written very simply, it is just one word.

doctype html
html
 head
 title
 body
The above code will be compiled into:

nbsp;html>

 
 <title></title>
 
 
jade uses strict indentation to distinguish the beginning and end of tags. The default is 2 spaces to indicate indentation.

If we want to write a label with content, for example, if we want to write a title, we only need to add a space after the label word, and then follow the content.

h1 this is a title.
p this is a paragraph.
The compilation result is:

this is a title.

this is a paragraph.

> ;
Sometimes, we will need to output some text in a special format or to improve the readability of the code, we need to display the following effect:

< ;p>

1. 001
2. 002
3. 003
4. 004


Then what we should do in jade How to write it? Here jade provides us with two methods. The first is to add a | and a space in front of each line:

p
 | 1. 001
 | 2. 002
 | 3. 003
 | 4. 004
The second method is: follow the label name with a . Number. Then the content under this tag will be parsed into a code segment by jade:

p.
 1. 001
 2. 002
 3. 003
 4. 004
Now some students are confused and confused. What is the difference between the two methods? Here we have to talk about tag mixing. If we have such a requirement, we need to add a strong tag after 1 in the above code.

First of all, let’s talk about the first case, our writing method:

p
 | 1. 001
 strong aaa
 | 2. 002
 | 3. 003
 | 4. 004
If it is the second writing method, we need to write it like this:

p.
 1. 001
 <strong>aaa</strong>
 2. 002
 3. 003
 4. 004
The compilation result is as follows :

1. 001
aaa
2. 002
3. 003
4. 004


##3. Tag’s attribute and attribute value##h1 p and other tags, we usually write id & class attributes for them, so how should this be written in jade? The same syntax as zen coding, we only need to write like this:

h1#id.class this is a title.
p#j-text.text this is a paragraph.
The compilation result is:

this is a title.

this is a paragraph.

等等,那我要是想添加多个 class 怎么办呢?这样办:

h1#id.class1.class2.class3 this is a title.
p#j-text.text this is a paragraph.

编译结果为:

this is a title.


this is a paragraph.

什么?写 p 写烦了?那就不写咯。

#id.class
#id.class1.class2 this is a p without tags.

编译结果为:


this is a p without tags.

这里要说明一下,在 jade 的语法里面,只有 p 标签能够省略不写.

说完了 id 和 class,我们再来说一下标签其他的属性应该怎么添加。jade 里添加其他属性和值的语法也和 zen coding 类似,我们需要在标签后面加上小括号(),然后按照(属性名=属性值)的格式写就好了,如果有多个属性,中间以逗号进行分割。

比如上面的 id 和 class 的写法我们就可以改写成:

h1(id="id", class="class") this is a title.
p(id="j-text", class="text") this is a paragraph.

结果是一样的:

this is a title.


this is a paragraph.

说来说去还是这两个属性,烦了?那我们换一个吧:

a(herf="/index.html", title="this is a link.", target="_blank", data-uid="1000") index.html

编译结果为:

index.html

那么问题就来了,如果我们要写一个单属性应该怎么写?比如给表单元素添加 checked属性:

input(type="checkbox", name="all", checked, value="全选")

编译结果为:

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

如何操作nodeJS服务器创建与重启

怎样进行Vue拖拽组件开发

The above is the detailed content of How to use node front-end template engine Jade tag. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Java vs JavaScript: A Detailed Comparison for DevelopersJava vs JavaScript: A Detailed Comparison for DevelopersMay 16, 2025 am 12:01 AM

JavaandJavaScriptaredistinctlanguages:Javaisusedforenterpriseandmobileapps,whileJavaScriptisforinteractivewebpages.1)Javaiscompiled,staticallytyped,andrunsonJVM.2)JavaScriptisinterpreted,dynamicallytyped,andrunsinbrowsersorNode.js.3)JavausesOOPwithcl

Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

See all articles

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

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool