Jade模板,如何将具体对象传递给页面? [英] Jade template, how to pass concrete object to pages?

查看:70
本文介绍了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.

这篇关于Jade模板,如何将具体对象传递给页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆