由于版权问题,现已改名pug.但无须担心,几乎没什么区别.就算依然使用jade也不会有太大影响. 慢慢迁移过渡即可

 
# 官网
https://pugjs.org # github
https://github.com/pugjs/pug # 文档地址
https://pugjs.org/language/inheritance.html # 入门指南
https://pugjs.org/api/getting-started.html
 

安装pug

 
# 全局安装cli
npm install pug-cli -g # 本地安装
npm install pug --save-dev
 

为了符合大众教材,依然使用jade也无伤大雅

 
# 创建文件夹和文件
mkdir jade-test && touch index.js index.jade # 安装依赖
npm init -y && cnpm install jade --save # 安装全局jade
cnpm install jade -g
 

index.jade

 
.header
h1 #{title}
p
.body
p #{body}
.footer
div #{By}
a(href="http://www.baidu.com/#{author.twitter}") #{author.name}
ul
each tag, index in tags
li #{tag}
 

index.js

 
var jade = require('jade')
var fs = require('fs') var data = {
title : "my title",
author: {
twitter: "@Lee",
name: "Azat"
},
tags: ['express', 'node', 'javascript']
} data.body = process.argv[2] // jade.compile
fs.readFile('index.jade', 'utf-8', function (error, source) {
var template = jade.compile(source);
var html = template(data)
console.log(html);
}) // jade.renderFile
jade.renderFile('index.jade', data, function (error, html) {
console.log(html)
})
 

运行jade: node index.js 'email body'

block 和 extends 、append(后) / prepend(前)

# API官方文档
https://pugjs.org/language/inheritance.html

layout.pug

 
doctype html
html(lang='en')
head
title= appTitle
body
block content
footer
block footer
| © 2009-2017 CyLeeBlog.com 版权所有 ICP证:粤S-172RM
 

index.pug

 
extends ./includes/layout.pug
block content
h1 #{title}
p Welcome to #{title}
block append footer
script
| window.alert('123')
 

login.jade

 
extends ./includes/layout.pug
block content
h1= title
form(method="post")
| 用户名:
input(name="name")
br
| 密码:
input(nane="pwd")
 

运行效果图如下: 可以看到不仅继承了 layout,在 content 块中添加了个性内容, 还成功的往 footer 块中添加了脚本,进一步灵活和个性化:

除了学到 block 和 extendsappend / prepend 的配合使用。还知道了。只要你不是变量,或者说你想以字符串开始。标签的后面必须是“”来声明。才能正常使用字符串或者 Javascript 脚本。而如果是字符串和变量嵌套,则变量需要使用#{变量}的形式书写。非常简单.


使用cli快速编译为html

(注:需要先安装全局pug-cli)

layout.pug

 
doctype html
html(lang='en')
head
title= title
body
h1= title
p Welcome to #{title}
ul
li
a(href="/") home
li
a(href="/login") login
li
a(href="/reg") reg
block content
footer
block footer
| © 2009-2017 CyLeeBlog.com 版权所有 ICP证:粤S-172RM
 

命令行输入:

 
# 查看帮助
pug --help # 编译为html
pug .\layout.pug -p layout.html # 编译并且赋值
pug .\layout.pug -O "{title: 'fuck you'}" .\layout.html # 监听并且实时改变html
pug .\layout.pug -w layout.html
 

if 条件判断

https://pugjs.org/language/conditionals.html

layout.pug

 
doctype html
html(lang='en')
head
title= title
body
h1= title
p Welcome to #{title}
ul
li
a(href="/") home
if user
li
a(href="/login") publish
li
a(href="/reg") logout
else
li
a(href="/login") login
li
a(href="/reg") reg
article
if success
div= success
if error
div= error
block content
footer
block footer
| © 2009-2017 CyLeeBlog.com 版权所有 ICP证:粤S-172RM
 

jade(pug)学习和使用的更多相关文章

  1. jade(pug)学习笔记(待填充.......)

    深刻认识到总结知识点的重要性,不然遇到似曾相识的问题,要翻老半天的百度才能解决.20171018 pug——文字内部嵌入结构 比如: <a class = "href"> ...

  2. pug学习

    pug学习 jade(pug)由于商标版权问题,jade已经改名为Pug.Pug 是一个高性能的模板引擎,它是用 JavaScript 实现的,并且可以供 Node 使用,当然还支持其他语言. 文件后 ...

  3. Jade入门学习笔记

    jade是超高性能的node JavaScript模板引擎,有着非常强大的API和大量杰出的特性.它主要针对node的服务端.由于商标的原因,改为Pug,哈巴狗.Pug有它本身的缺点--可移植性差,调 ...

  4. [Pug] Template Engine -- Jade/ Pug

    Looking at the follow code: .wrapper - const upName = name && name.toUpperCase(); h2 | Hello ...

  5. flag - 待浏览学习网站

    学习:gulp+jade(pug)+sass 待浏览网站如下:http://www.ydcss.com/archives/18#lesson1 https://nodejs.org/en/ https ...

  6. 7、 jade 、 ejs、express集成模板

    jade/ejs 模板引擎 http://jade-lang.com/ http://www.nooong.com/docs/jade_chinese.htm SSR 服务器端渲染 服务器生成html ...

  7. Express全系列教程之(十):jade模板引擎

    一.前言 随着前端业务的不断发展,页面交互逻辑的不断提高,让数据和界面实现分离渐渐被提了出来.JavaScript的MVC思想也流行了起来,在这种背景下,基于node.js的模板引擎也随之出现. 什么 ...

  8. nodeJS学习(3)--- npm 配置和安装 express4.X 遇到的问题及解决

    前言:懒得看前面两篇介绍的也可以从本节直接参考,但建议最好了解下,因为 4.X 的express 已经把命令行工具分离出来 (链接https://github.com/expressjs/genera ...

  9. vscode 配置 Pug Compile Hero Pro 插件步骤

    这个随笔主要介绍 vscode 配置 Pug Compile Hero Pro 插件的步骤,实现快速使用less 以及 scss 等的编程语言 第一步 当然是安装我们的插件啦! 在插件商店里 搜 Sa ...

  10. Express全系列教程之(一):Express的安装 和第一个程序

    前言 ndoe.js,一个基于javsscript运行环境的服务器语言,它的出现使得javascript有能力去实现服务器操作.在gitHub上ndoe.js的star数已接近6万,可见其受欢迎程度: ...

随机推荐

  1. 【转载】关闭XenServer中挂起(hang)虚机的方法

    在XenServer中,碰到VM挂起(hang)的情况,也不是那么少见,而VM长时间挂起,那么很影响心情和后续的操作. 一般情况下,为了关闭VM或者重启VM,我们推荐这样的操作顺序: 进入到VM内,使 ...

  2. 学习windows编程 day4 之 多边矩形填充

    #include <windows.h> #include <math.h> LRESULT CALLBACK WndProc(HWND hwnd, UINT message, ...

  3. hive vs hbase

    HIVE和HBASE区别 两者分别是什么? Apache Hive是一个构建在Hadoop基础设施之上的数据仓库.通过Hive可以使用HQL语言查询存放在HDFS上的数据.HQL是一种类SQL语言,这 ...

  4. C# 实现线段的编码裁剪算法(vs2010)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. Java EE 之Hibernate异常总结【1】org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    字面意义就是不能被初始化. 简单理解就是因为,你使用了lazy=true,这样hibernate在从数据库中调数据的时候是不会把关联的对象查出来的,而是保存一个获取值得方法,在你使用getXXX()调 ...

  6. MySQL5.7主从复制配置

    1 my.cnf文件 配置 binlog_format = ROW log_bin_trust_function_creators=1 log-error = /usr/local/mysql/dat ...

  7. ZOC7 for Mac连接CentOS7无法输入中文问题

    确定是ZOC7的问题 改为 iTerm2 加 ZSH 能够输入中文了 自己配置profile 慢慢所有的终端都用iTerm2 渐渐放弃ZOC7

  8. saltstack系列~第四篇

    简介 针对mysql的sls编写0 软件包推送部分  tool_rsync:     file.recurse:        - source: salt://files/mysql        ...

  9. 终端命令行开启和关闭mac隐藏文件

    defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.apple.finde ...

  10. ubuntu 禁用 nouveau 方法

    (1)直接移除这个驱动(备份出来) $ mv /lib/modules/4.4.0-31-generic/kernel/drivers/gpu/drm/nouveau/nouveau.ko /lib/ ...