Egg: 前台页面直接输出后台JSON数据

Created on 5 Jan 2018  ·  3Comments  ·  Source: eggjs/egg

  • Node Version:
  • Egg Version:
  • Plugin Name:
  • Plugin Version:
  • Platform:
  • Mini Showcase Repository:


image
按照文档写的这样页面编译就会错,加上双引号 是显示Object ,如何在前端页面直接输出后台的JSON数据?

Most helpful comment

文档里写了,不加 safe 输出的是 encode 之后的内容 😀

All 3 comments

文档里使用的是 nunjucks 作为模板引擎

暂未发现问题,测试代码如下:

  • controller
// app/controller/test.js
const Controller = require('egg').Controller;

class TestController extends Controller {
  async index() {
    await this.ctx.render('home.nj', { title: 'Home', data: { "a": { "b": 1} } });
  }
}

module.exports = TestController;
  • view
// app/view/test.nj
<script>
  const data = {{ helper.sjson(data) | safe }}
</script>
  • output
<script>
  const data = {"a":{"b":1}}
</script>

可以了,发现不加 safe 就不行,感谢

文档里写了,不加 safe 输出的是 encode 之后的内容 😀

Was this page helpful?
0 / 5 - 0 ratings