Фронт 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 ответ
Решение
Обновить
Этот пример я только что написал (называется 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>