YAML Jekyll的前端问题和嵌套列表

时间:2012-10-06 15:35:23

标签: yaml jekyll

我有一组嵌套的yaml列表,其中包含以下内容:

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

我尝试过使用for subitem in item和其他选项 - 但我无法让它发挥作用 - 任何想法?

1 个答案:

答案 0 :(得分:41)

更新

我刚写的这个例子(称为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>