search

Home  >  Q&A  >  body text

node.js - Express + Jade在解析json的时候,html标签也当成普通字符串了,怎么解决呢?

//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_leeringa_lee2785 days ago653

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 11:17:51

    使用 p.content!= val.post

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

    reply
    0
  • PHPz

    PHPz2017-04-17 11:17:51

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

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

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 11:17:51

    I also want to ask this question. I didn’t use jade, and what express threw out was a string directly

    reply
    0
  • 黄舟

    黄舟2017-04-17 11:17:51

    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/

    reply
    0
  • Cancelreply