我是新来的十一,并试图创建一个看似简单的网站结构,但我正在挣扎,所以会感谢任何帮助。
我有以下数据:
{
authors: [
{
name: "John Smith",
books: [
{
title: "John's book 1",
price: 9.99
},
{
title: "JS book 2",
price: 10.99
}
]
},
{
name: "Mickey Mouse",
books: [
{
title: "Mice to meet you",
price: 7.99
}
]
}
]
}对于这些数据,我希望创建以下页面层次结构:
/authors
/authors/john-smith
/authors/john-smith/johns-book-1
/authors/john-smith/js-book-2
/authors/mickey-mouse
/authors/mickey-mouse/mice-to-meet-you我已经创建了/authors、/authors/john-smith和/authors/mickey-mouse页面,但是,我正在与“图书”页面进行斗争。
我不知道如何创建页面,我也希望在页面上显示作者的名字,也不知道如何访问数据。
例如,/authors/mickey-mouse/mice-to-meet-you应包含以下内容:
<h1>Mice to meet you</h1>
<h2>By Mickey Mouse</h2>谢谢
发布于 2022-06-02 04:38:48
我可能会通过创建一个全局JavaScript数据文件来处理这个问题,它可以将作者的数据操作到一个图书列表中:
// _data/books.js (for example)
const data = require('./data.json')
module.exports = function () {
// `reduce` takes in an accumulator (empty array in this case)
// and then iterates through the array
const books = data.authors.reduce((acc, author) => {
// for every author, we look at every book
author.books.forEach((book) => {
// add the book to the new array, adding in the author's name
acc.push({author: author.name, ...book})
})
// return the accumulator for the next iteration
return acc
}, [])
// the data now looks like
// [ { author: "John Smith", title: "John's book 1", ... }, ... ]
return books
}我们现在可以通过这一系列书籍进行分页:
---
pagination:
data: books # assuming the data file is books.js
size: 1
alias: book
permalink: "/authors/{{ book.author }}/{{ book.title }}/"
---
# {{ book.title }}
## By {{ book.author }}(上面的示例假设您在Nunjucks模板引擎中使用Markdown,但是它可以处理您所使用的任何东西。)
https://stackoverflow.com/questions/72318897
复制相似问题