node.js - Express + Jade在解析json的时候,html标签也当成普通字符串了,怎么解决呢?
ringa_lee
ringa_lee 2017-04-17 11:15:51
0
4
649

//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);
...
ringa_lee
ringa_lee

ringa_lee

reply all(4)
洪涛

使用 p.content!= val.post

See http://jade-lang.com/reference/code/

PHPzhong

It is more customary to use #{locals.xxx}.

Then !{locals.xxx} is written without escaping and will be escaped by default

Peter_Zhu

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:

-items.forEach(function(item) {
    h2
        a(href="#")=item.title
    

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/

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template