从 JSON 的文件夹生成 Jekyll 页面

Generating Jekyll pages from a folder of JSON

所以我在一个文件夹中有一堆鸡尾酒配方,每个都有不同的名称,例如 screwdriver.json 或 adult-hot-chocolate.json 以及相应命名的图像,每个配方看起来像这样:

{
"name": "Adult Hot Chocolate",
"description": "Appropriately named 'adult hot chocolate', this recipe is one of the easiest and most common ways to spike your cocoa. To make it, you will simply add a shot of peppermint schnapps to your favorite hot chocolate and a delicious, warm drink is yours to enjoy.",
"github": "thesanjeevsharma",
"ingredients": [
    {
        "quantity": "2",
        "measure": "oz",
        "ingredient": "Peppermint Schnapps"
    },
    {
        "quantity": "6",
        "measure": "oz",
        "ingredient": "Hot Chocolate"
    },
    {
        "quantity": "",
        "measure": "",
        "ingredient": "Garnish: ​whipped cream"
    },
    {
        "quantity": "",
        "measure": "",
        "ingredient": "Garnish: chocolate sprinkles or shaved chocolate"
    }
],
"directions": [
    "Gather the ingredients.",
    "Pour the schnapps into a warm mug or Irish coffee glass.",
    "Fill with hot chocolate.",
    "Stir well.",
    "Optionally, top with whipped cream and chocolate sprinkles or shaved chocolate.",
    "Serve and enjoy!"
],
"image": "adult-hot-chocolate.jpg",
"keywords": [
    "chocolate",
    "peppermint",
    "schnapps"
]
}

我想创建一个布局并随机选择一个食谱和图片显示在我的网站上。如果它们都在一个大的 YAML 文档中,我知道该怎么做,但我不清楚如何告诉 Jekyll 解析单独的文件以便我可以随机引用一个文件。

另一种可能性是将它们合并成一个大的 json 但是我不能简单地从原始来源中提取新的食谱。

如有任何帮助,我们将不胜感激!

请查看jekyll data documentation

所以您必须创建一个 _data 文件夹,如果它还不存在的话。

然后每个 cocktail 复制一个 json 文件到子文件夹中,例如_data/cocktails.

要阅读鸡尾酒列表,您必须使用 html 或降价内容创建 page

html的内容可能是这样的

---
layout: default
title: "My Cocktails"
---

<ul>
{% for file in site.data.cocktails%}
{% assign cocktail = file[1] %} {% comment %}The assign is needed, because we have different json files!{% endcomment %}
  <li><strong>{{ cocktail.name }}</strong> - {{ cocktail.description}}</li>
{% endfor %}
</ul>