search
HomeWeb Front-endJS TutorialNode.js template engine jade example explanation

Node.js template engine jade example explanation

Feb 06, 2018 am 09:19 AM
javascriptnode.js

This article mainly brings you a tutorial based on Node.js template engine-jade quick learning and practical combat 1. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor to take a look, I hope it can help everyone.

Environment preparation:

Install jade globally: npm install jade -g

Initialize project package.json: npm init --yes

Install After completion, you can use jade --help to view the command line usage of jade

1. Create a new index.jade file in the project directory

inde.jade code:

doctype html
html
  head
    meta(charset='utf-8')
    title
  body
    h3 欢迎学习jade

1. The label should be written in the indented format of HTML

2. The attribute of the label can be in parentheses

3. If the label has content, it can be written directly after the label

Then use jade -P index.jade on the command line to compile the index.jade file into an index.html file. -P (organizes the code into an indented format. Without this parameter, index.html will be in compressed format, not Conducive to reading)

Compiled index.html code:

nbsp;html>

 
  <meta>
  <title></title>
 
 
  <h3 id="欢迎学习jade">欢迎学习jade</h3>
 

2. Class, id and other attributes and writing of multi-line text

Create a new index2.jade file, The code is as follows:

doctype html
html
  head
    meta(charset='utf8')
    title jade template engine
  body
    h1 jade template engine
    h1 jade template engine
    h1 jade template engine
    h1 jade template engine
    p#box.box1.box2(class='box3')
    #abc.box1.box2.box3
    h3.box1.box2(class='abc def')
    a(href='http://www.taobao.com',
    target = 'blank') 淘宝
    input(type='button', value='点我')
    br
    p.
      1,this is
      <strong>hello</strong>
      2,test
      3,string
    p
      |  1, this is
      strong hello
      |  2, test
      |  3, test string

Execute the compilation command: jade -P ghostwu.html Compile index2.jade into the ghostwu.html file. The compiled code is as follows:

nbsp;html>

 
  <meta>
  <title>jade template engine</title>
 
 
  <h1 id="jade-template-engine">jade template engine</h1>
  <h1 id="jade-template-engine">jade template engine</h1>
  <h1 id="jade-template-engine">jade template engine</h1>
  <h1 id="jade-template-engine">jade template engine</h1>
  <p></p>
  <p></p>
  <h3></h3><a>淘宝</a>
  <input><br>
  <p>
   1,this is
   <strong>hello</strong>
   2,test
   3,string
  </p>
  <p> 1, this is<strong>hello</strong> 2, test
    3, test string
  </p>
 

1. p#box.box1.box2(class='box3') This way of writing is the way of writing emmet. # is the id attribute point (.) which is the class attribute bracket and is also the way of writing the attribute

2, #abc.box1 .box2.box3, there is no element tag name at all, the default is to add these attributes to the p tag

3, two ways of writing multi-line text

p.

1,this is
hello
2,test
3,string

The first way to write multi-line text: the p tag should be followed by a. inside Use the original html tag writing method

p

| 1, this is
strong hello
| 2, test
| 3, test string

The second way to write multi-line text: use a vertical bar (|) in front of the text, followed by the content

3. Comment

jade template code:

doctype html
html
  head
    meta(charset='utf8')
    title jade模板引擎学习-by ghostwu
  body
    h3 单行注释
    // p.box.box2 这是一段p
    h3 不会编译到html文件的注释
    //- p#box.box2.box3
    h3 块注释,也叫多行注释
    //- 
      input(type='checkbox', name='meinv', value='仙女') 仙女
      input(type='checkbox', name='meinv', value='御姐') 御姐
    h3 这里不是注释
    input(type='checkbox', name='meinv', value='仙女')
    | 仙女
    input(type='checkbox', name='meinv', value='御姐')
    | 御姐

After compilation:

nbsp;html>

 
  <meta>
  <title>jade模板引擎学习-by ghostwu</title>
 
 
  <h3 id="单行注释">单行注释</h3>
  <!-- p.box.box2 这是一段p-->
  <h3 id="不会编译到html文件的注释">不会编译到html文件的注释</h3>
  <h3 id="块注释-也叫多行注释">块注释,也叫多行注释</h3>
  <h3 id="这里不是注释">这里不是注释</h3>
  <input>仙女
  <input>御姐
 

1, single line comment

// p.box.box2 This is a p

2, which is only commented in jade and will not be compiled into the html file

//- p#box.box2.box3

3, block comment (multi-line text comment), the commented content should be on a new line

4, after the checkbox Pay attention to the display text part, do not place it next to the attribute, start a new line and write it after the vertical line

4. jade template actual menu

doctype html
html
  head
    meta(charset='utf8')
    title jade模板引擎学习-by ghostwu
    style.
      * { margin : 0; padding: 0; }
      li { list-style-type: none; }
      a { text-decoration: none; color: white; }
      #nav { width:980px; height: 34px; margin:20px auto; line-height:34px; background:#800;}
      #nav li { float:left; }
      #nav li.active { background:red; }
      #nav li:hover { background:#09f; }
      #nav li a{ padding: 5px 10px; }
  body
    p#nav
      ul
        li.active
          a(href='javascript:;') 首页
        li
          a(href='javascript:;') 玄幻小说
        li
          a(href='javascript:;') 修真小说
        li
          a(href='javascript:;') 都世小说
        li
          a(href='javascript:;') 科幻小说
        li
          a(href='javascript:;') 网游小说

Compile ( jade index.jade The effect after -P -w ): -w: Monitor the modification of the file in real time, and refresh the results immediately after saving, which is the hot loading technology that is very popular in modern front-end development

# #5. Interpretation of variables

doctype html
html
  head
    meta(charset='utf8')
    - var title = 'jade模板引擎学习-by ghostwu';
    title #{title.toUpperCase()}
    style.
      * { margin : 0; padding: 0; }
      li { list-style-type: none; }
      a { text-decoration: none; color: white; }
      #nav { width:980px; height: 34px; margin:20px auto; line-height:34px; background:#800;}
      #nav li { float:left; }
      #nav li.active { background:red; }
      #nav li:hover { background:#09f; }
      #nav li a{ padding: 5px 10px; }
  body
    p#nav
      ul
        li.active
          a(href='javascript:;') 首页
        li
          a(href='javascript:;') 玄幻小说
        li
          a(href='javascript:;') 修真小说
        li
          a(href='javascript:;') 都世小说
        li
          a(href='javascript:;') 科幻小说
        li
          a(href='javascript:;') 网游小说
#{}: Variables can be interpreted, toUpperCase(): Variables are converted to uppercase

Pass the data of the json file to the template during compilation,

Create new data.json file data

{
"content" : "跟着ghostwu学习jade"
}
index2.jade文件模板:
doctype html
html
  head
    meta(charset='utf8')
    - var title = 'jade模板引擎学习-by ghostwu';
    title #{title.toUpperCase()}
  body
    h3 #{content}
Compile command: jade index2.jade -P -O data.json -O Specify a json file and transfer the data of the json file to the template

Compiled result:

nbsp;html>

 
  <meta>
  <title>JADE模板引擎学习-BY GHOSTWU</title>
 
 
  <h3 id="跟着ghostwu学习jade">跟着ghostwu学习jade</h3>
 
Related recommendations:


Detailed explanation of Node.js template engine Jade

The above is the detailed content of Node.js template engine jade example explanation. 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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.