Jekyll 和嵌套列表的 YAML 前端 [英] YAML front matter for Jekyll and nested lists

查看:19
本文介绍了Jekyll 和嵌套列表的 YAML 前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组嵌套的 yaml 列表,如下所示:

I have a set of nested yaml lists with something like the following:

title: the example
image: link.jpg
products:
 - top-level: Product One
   arbitrary: Value
   nested-products:
    - nested: Associated Product
      sub-arbitrary: Associated Value
 - top-level: Product Two
   arbitrary: Value
 - top-level: Product Three
   arbitrary: Value

我可以使用 for item in page.products 毫无问题地遍历产品,并且我可以使用逻辑运算符来确定是否存在嵌套产品 - 我不能做的是遍历多个nested-products 每次迭代 top-level

I can loop through the products with no problem using for item in page.products and I can use a logic operator to determine if nested products exist - what I CAN'T do is loop through multiple nested-products per iteration of top-level

我已尝试使用 for subitem in item 和其他选项 - 但我无法让它工作 - 有什么想法吗?

I have tried using for subitem in item and other options - but I can't get it to work - any ideas?

推荐答案

更新

我刚刚写的这个例子(叫做index.html)

Update

This example I just wrote (called index.html)

---
title: the example
products:
 - top-level: Product One
   arbitrary: Value
   nested-products:
    - nested: Associated Product
      sub-arbitrary: Associated Value
    - nested: Another associate
      sub-arbitrary: with its associated value
 - top-level: Product Two
   arbitrary: Value
   nested-products:
    - nested: nested product Two
      sub-arbitrary: Two's nested's associate value
 - top-level: Product Three
   arbitrary: Value
 - top-level: Product Four
   arbitrary: SomeValue
---
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">

<head>
  <title>{{ page.title }}</title>
</head>

<body>

<h4>products:</h4>
<ul>{% for product in page.products %}
  <li>{{ product.top-level }}: {{ product.arbitrary }}{% if product.nested-products %}
    <ul>
    {% for nestedproduct in product.nested-products %}  <li>{{ nestedproduct.nested }}: {{ nestedproduct.sub-arbitrary }}</li>
    {% endfor %}</ul>
  {% endif %}</li>{% endfor %}
</ul>

<p>Hope that answers it</p>

</body>
</html>

产生这个:

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">

<head>
  <title>the example</title>
</head>

<body>

<h4>products:</h4>
<ul>
  <li>Product One: Value
    <ul>
      <li>Associated Product: Associated Value</li>
      <li>Another associate: with its associated value</li>
    </ul>
  </li>
  <li>Product Two: Value
    <ul>
      <li>nested product Two: Two's nested's associate value</li>
    </ul>
  </li>
  <li>Product Three: Value</li>
  <li>Product Four: SomeValue</li>
</ul>

<p>Hope that answers it</p>

</body>
</html>

这篇关于Jekyll 和嵌套列表的 YAML 前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆