【问题标题】:How to loop over posts in Hexo with native Jade construction如何使用原生 Jade 构建在 Hexo 中循环帖子
【发布时间】:2014-02-16 13:54:44
【问题描述】:

我知道,我可以使用这样的东西:

- site.posts.each(function(article){
      h2.title= article.title
      p.article= arcticle.content
- })

我觉得这种方式不对,因为 Jade 有自己的原生构造来循环某些东西,但它似乎不起作用:

each article in site.posts
     h2.title= article.title
     p.article= arcticle.content

【问题讨论】:

  • 您能否提供更多细节,为什么您认为第二个代码 sn -p 不起作用?
  • 我不知道为什么它不起作用。我所拥有的只是我看到了第一个 sn-p 的结果,并看到该文章没有用第二个定义。我想 site.post 返回的数据结构与原生玉循环不完全兼容,但我不知道如何处理。
  • each article in array 是正确的说法。如果 Jade 无法处理,它可能会抛出错误。您能否检查site.posts 是否是您的请求处理程序中的一个数组,例如,使用util.isArray(site.posts)。或者将站点对象的内容打印到控制台 (util.inspect(site, { showHidden: true, depth: 3 })) 并检查 posts 是否打印为数组 ([])
  • Posts 不是一个数组,而是一些带有_index_populates 键的奇怪字典对象......这只是......奇怪。

标签: node.js pug


【解决方案1】:

您需要执行site.posts.toArray() 并且循环将正常工作。这也让我迷惑了一阵子,github上的答案大多是中文的。

https://github.com/tommy351/hexo/issues/321

【讨论】:

    猜你喜欢
    • 2015-03-09
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多