注意事项
- 使用空格来缩进(使用Tab键,容易出问题)
- 尽量使用
p #{obj} #{obj}
避免使用 p=obj=obj
- 关键词前面一律不加
-
如 each、while、else …
环境安装
1. 实时编译
npm install -g jade
jade -P -w index.jade
2. Nodejs 中使用
const http = require('http');
const jade = require('jade');
const server = http.createServer();
server.on('request', (req, res)=>{
var jadeStr = "a(href='one.html') 栏目one"
let one = jade.compile(jadeStr, {pretty:true})();
console.log(one);
let two = jade.render(jadeStr);
console.log(two);
let three = jade.renderFile('./one.jade',{pretty:true});
res.end(three)
})
server.listen(8080);
3. Express 框架中使用
const express = require('express');
const app = express();
const jade = require('jade');
app.set('views','view')
app.set('view engine','jade')
app.get('/', (req, res) => {
res.render('home')
})
语法 API
- Jade是变体的HTML,使用
缩进
来描述元素的层级从属关系
1. doctype html
doctype html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"
doctype html
2. 注释
- 注释,使用
//
,如需注释多行,请缩进注释内容即可 - 不编译注释 ,使用
//-
- 条件注释(if IE)
3. id & class
div.className
div.idName
div.className
#idName.className
4. 标签属性
- 设置标签属性,当属性值是
undefined
或者 null
时不会被加上
a(href='index.html', title='home')
a(
href='index.html'
title='home')
<a href="index.html" title="home"></a>
rss(xmlns:atom="atom")
var user = { id: 12, name: 'hannah' }
a(href='/user/' + user.id)= user.name
a(href='/user/#{user.id}')= user.name
- 布尔属性,默认为true,若设值为
false
,则不会加入该属性。
input(type='checkbox', checked=false)
<input type="checkbox">
div=
<div>I am<b>very well</b>.</div>
div!=
<div>I am <b>very well</b>.</div>
img(src='one.jpg')/
<img src='one.jpg' />
ul(style={list-style:none,margin:'5px 10px'})
div(class=['red', 'blue'])
5. 标签文本
p hannah
<p>hannah</p>
p.
hannah
logan is wolverine
script.
var obj = {
name: 'hannah',
age: 18
};
console.log(obj);
<p>
hannah
logan is wolverine
</p>
<script>
var obj = {
name: 'hannah',
age: 18
};
console.log(obj);
</script>
p
|hannah is
|Shy girl
<p>hannah is Shy girl</p>
6. 内联 & 块展开
ul
li.first
a(href='#') contA
li
a(href='#') contB
li.last
a(href='#') contC
- 块展开可以在一行内创建嵌套的标签,使用
:
标识子级
ul
li.first: a(href='#') contA
li: a(href='#') contB
li.last: a(href='#') contC
7. Case 表达式
friends = 5
case friends
when 0: p you have no friends
when 1: p you have a friend
default: p you have #{friends} friends
参考引用:Jade —— 源于 Node.js 的 HTML 模板引擎