带有 Express 的 PUG 中的高级占位符

Advanced Placeholder in PUG with Express

我知道 PUG 中的标准占位符系统是如何工作的。

res.render('index', { title: 'Chat'}

将在 #{title}

我也从 API 中获取我的对象,比如

res.render('index', { title: 'Chat', name: obj[1].pseudo});

作为#{name}

工作

但是我的问题来了。

我希望在 PUG 中有像 #{user:6:name} 这样的占位符,它将是 obj[6]pseudo

有这样的方法吗?

还是另一个? 我有大约 60 个个人资料数据,我想在模板中尽可能灵活地使用它们。

这是我的 index.js

    /* GET home page. */
router.get('/', function(req, res, next) {
    Request.get("https://cmediaapi.datingpartner.com/content/v3_real/profiles", (error, response, data) => {
      if(error) {
          return console.dir(error);
      }
      // console.dir(JSON.parse(data));
      obj = JSON.parse(data);
      // console.dir(obj[1].pseudo);
      res.render('index', { title: 'Chat', name: obj[1].pseudo});
    });
});

非常感谢您的帮助和想法!

Pug interpolation 使用 Javascript 表达式。

将当地人传递为:

res.render('index', { title: 'Chat', name: obj });

使用标准 javascript 访问器:

#{name[1].pseudo}

我还发现这个在其他情况下可能会有帮助。

当您想在循环中使用特殊数量的用户 ID 时。

此示例从用户 ID 10 开始,到用户 ID 40 结束。

ul#list
        //- li.user #{user[2].pseudo}       
        each user in obj.slice(10, 40)
            li(class='user')
                div.thumb
                    img(src='https://images.url.com/' + user.image_300x300)
                div.info(onclick="location.href=\'/user/" + user.pseudo + "?" + ref + "'")
                    p= user.pseudo
                    p #{user.age} Jahre | #{user.city}