//postList.json
[
{
"title": "天文学家发现'巨型地球'",
"date": "2014.06.08",
"post": "<p>大自然总是会让人捉摸不透</p><p>发现第一颗外星行星,不过是20年前的事情。在这一领域,我们依然是初学者。</p><p>我们才刚刚开始发现惊奇。在茫茫宇宙之中,还有更多古怪之地等着我们去发现。</p>"
},
{
"title": "天文学家发现'巨型地球'",
"date": "2014.06.08",
"post": "<p>大自然总是会让人捉摸不透</p><p>发现第一颗外星行星,不过是20年前的事情。在这一领域,我们依然是初学者。</p><p>我们才刚刚开始发现惊奇。在茫茫宇宙之中,还有更多古怪之地等着我们去发现。</p>"
}
]
//postlist.jade
article.site-list
- each val in posts
section.list
h3.title= val.title
p.date
span 发表于
time= val.date
p.content= val.post
//index.js
var express = require('express');
var router = express.Router();
var datapostlist = require('../data/postList.json');
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', {
title: 'jaySite',
userName: 'Jay.Creater',
userEmail: 'jaycreater@163.com',
posts: datapostlist
});
});
module.exports = router;
//app.js
...
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(require('node-compass')({mode: 'expanded'}));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
...
使用
p.content!= val.post
See http://jade-lang.com/reference/code/
It is more customary to use #{locals.xxx}.
Then !{locals.xxx} is written without escaping and will be escaped by default
I also want to ask this question. I didn’t use jade, and what express threw out was a string directly
If it is an array object passed from the server, such as items = [{title: 'hello'}, {title: 'b'}]
To display it:
Since Jade escapes tags and scripts in HTML for protection, use != to prevent escaping:
p!=item.title
Please refer to jade’s documentation: http://jade-lang.com/reference/code/