简体   繁体   English

如何使用Pug / Jade显示外部JSON?

[英]How do I display external JSON using Pug/Jade?

I am trying to figure out how to use pug to iterate through a JSON object retrieved via a GET request from an API. 我试图弄清楚如何使用pug来遍历通过API的GET请求检索的JSON对象。 Can anyone point me to a resource or provide an example of making a GET request and then iterating through an object within a Pug template? 谁能指出我的资源或提供一个发出GET请求,然后遍历Pug模板中的对象的示例? I have been looking for documentation on this to no avail. 我一直在寻找有关此文档无济于事。

I know how to do this in handlebars but is it possible in PUG/Jade? 我知道如何在车把上这样做,但是在PUG / Jade中可以吗?

Any help appreciated 任何帮助表示赞赏

I think this is what you want, in index.js: 我认为这是您想要的,在index.js中:

    router.get('/', function(req, res, next) {
        var json =[{id:1,name:foo,},{id:2, name:foooo}]
        res.render('index',{title: 'title', json});
    });

    module.exports = router;

Then in your index.pug, you can iterate over the object json like this: 然后在index.pug中,您可以像这样遍历对象json:

each elem,i in json
    p !{elem.id}-!{elem.name}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM