首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >11& pug:通过循环查看pug博客帖子来创建博客列表

11& pug:通过循环查看pug博客帖子来创建博客列表
EN

Stack Overflow用户
提问于 2021-03-21 20:45:56
回答 1查看 232关注 0票数 0

我有一个用帕格模板语言写的博客文章列表。每个博客文章都有一个正面的标题。例如:

代码语言:javascript
复制
---
id: "achilles-turtle-metapost"
title: "The achilles & turtle metapost"
date: "2019-08-14"
img: "blue-liquid.png"
tags: ["Achilles & Turtle", "Philosophy"]
excerpt: "Who is this achilles and his calm friend the turtle? And what are they talking about every day?"
---
p Some very simple content of this blog post.
p Dummy second paragraph

使用静态站点生成器十一解析模板。

博客-列表.帕格看起来是这样的。变量collections.all被十一个博客文章正确地填充了数据。

代码语言:javascript
复制
- for (var postIdx = 0; postIdx < collections.all.length; postIdx++)
  include blog-list-item.pug

我的问题是: blog-list-item.pug需要for循环中当前元素的数据。如何访问blog-list-item.pug部分中的数据?

我已经试过了,但不起作用。(这适用于朴素的帕格):

代码语言:javascript
复制
- for (var postIdx = 0; postIdx < collections.all.length; postIdx++)
  - var post = collections.all[postIdx].data   // this is a reference to the frontmatter data
  include blog-list-item.pug     

当使用朴素pug时,post变量在blog-list-item.pug中可用。帕格将JavaScript上下文传递给包含的模板。但不是在用十一号的时候。

我遗漏了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-06-15 14:44:45

您可以使用for...of循环访问collections.all数组中的数据项,而不仅仅是遍历索引。

例:

代码语言:javascript
复制
- for (const post of collections.all)
  include blog-list-item.pug     

然后,根据设置部分的方式,您可能只需使用包含来访问这些变量--如果遇到问题,可能会看到提示这里)。

或者,您可以将mixins作为参数传递给变量,而不是部分变量:

代码语言:javascript
复制
   +blog-list-item(post)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66737351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档