Jade模板,如何将具体对象传递给页面?

Jade模板,如何将具体对象传递给页面?

问题描述:

我为我的node.js项目提供了一个Jade模板.我想将一个对象发送到jade模板,并将其传递给页面内的一个函数(以呈现某些内容).

I have a jade template for my node.js project. I would like to send an object to the jade template and pass it to a function inside the page (to render something).

我确定我是从服务器这样发送正确的内容的

I am sure I send the right stuff from the server like this

res.render(__dirname + '/pages/viz.jade', {
    vizJson: newJson,
});

在客户端中,我做这样的事情:

in the client I do something like this:

script
    sunburst(#{vizJson})

因此,在脚本函数中,我想调用一个函数,该函数使用在服务器端创建的json创建可视化.

Thus, inside a script function, I want to call a function that creates my visualization with some json I created on the server side.

问题是,渲染时我有类似sunburst([Object object])的东西.我也尝试发送JSON的字符串化版本,但是当我JSON.parse(#{vizJson})执行此操作时,它会抱怨Unexpected token &.

The problem is that when rendered I have something like sunburst([Object object]). I also tried to send the stringified version of the JSON but when I do JSON.parse(#{vizJson}) it complains like Unexpected token &.

我发送的json总是不同,深度也不同.

The json I send is always different and has different level of depths.

有人知道该怎么做吗?

谢谢

我希望这将对某人有所帮助.我是这样解决的:

I hope this is going to help someone. I solved it like this:

script
    sunburst(!{JSON.stringify(vizJson)})

请注意!{...}包装了stringify方法.

Notice the ! and the {...} wrapping the stringify method.