我有一个用 pug 模板语言编写的博客文章列表。每篇博文都有一个 frontmatter 标题。例如:
---
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
模板由静态站点生成器Eleventy解析
blog-list.pug 看起来像这样。该变量collections.all
正确填充了 Eleventy 的有关我的博客文章的数据。
- for (var postIdx = 0; postIdx < collections.all.length; postIdx++)
include blog-list-item.pug
我的问题: blog-list-item.pug 需要for 循环中当前元素的数据。如何在 blog-list-item.pug 部分中访问它?
我已经尝试过了,但不起作用。(这适用于普通哈巴狗):
- 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
使用普通哈巴狗时,该post
变量在内部可用。blog-list-item.pug
JavaScript 上下文由 pug 传递给包含的模板。但不是在用 Eleventy 做所有这些的时候。
我错过了什么?