You are almost there! I think a good solution is to use another for
loop in your code. I took your data file and played around with it a bit, so I did reformat the structure, mostly because it is how I work with YAML files, so feel free to re-format as you see fit.
In future posts, please use proper code blocks as your code was not easy to read/copy/paste since it came through formatted as HTML.
I modified your dataset to look like this:
items:
- number: "01"
start: May 31
name: Introduction
description: This is a description
image: "/images/conduct.jpg"
objectives:
- "Explain the importance of services to a country’s economy."
- "Demonstrate how services are defined using the concept of non-ownership."
- "Classify a company as one of four common types of business services."
topics:
- "Topic #1"
- "Topic #2"
- "Topic #3"
- number: "02"
start: June 04
name: After introduction
description: This is a second description
image: "/images/conduct2.jpg"
objectives:
- "Second objective, line 1"
- "Second objective, line 2"
- "Second objective, line 3"
topics:
- "Second Topic #1"
- "Second Topic #2"
- "Second Topic #3"
Then I created two for
loops to address the formatting you want:
{%- assign modules = site.data.modules.items | where: "number", 01 -%}
{%- for module in modules -%}
<p>Module: {{ module.number }}</p>
{%- if module.objectives -%}
<ul>
{%- for objective in module.objectives -%}
<li>{{- objective -}}</li>
{%- endfor -%}
</ul>
{%- endif -%}
{%- endfor -%}
By using assign
, you can read the dataset and add a Liquid where
clause, allowing you to get rid of that extra if
statement you have in the original code. If you were to remove the where
clause in the assign
declaration, all the modules and objectives will display.
The first for
loop displays the module number or any other details you want (start, name, etc.).
While probably not 100% necessary, I added an if
statement to make sure objectives exist in the data. If you remove that if
statement and there are no objectives, then there will be a dangling <ul></ul>
block in the final HTML source code.
The starting <ul>
tag starts an unordered list. The second for
loop displays all the objectives as list items by using an <li></li>
block. After the for
loop, the objectives are closed out with an ending </ul>
tag.
Please note I had a super hard time with that where
clause in the past. Code samples say you can type the code like this:
where: "number", "01"
But that code won’t work. You need to remove the quotes for the module number, like this:
where: "number", 01
I’m sure it is my lack of YAML knowledge that is to blame, but do pay extra close attention to that when it comes to a number. Of course where: "start", "May 31"
works just fine, so go figure .
You might want to consider using collections instead of data. The reason collections are useful is each module will be a markdown or HTML file. That is useful because when you edit them in standard markdown using a browser and you will get spelling and grammar checks. You can also add whatever tailored content that may not come with using hard-coded data. Just a thought and certainly not a requirement.
Finally, I created a series of videos that demonstrate how to work with data in Jekyll. It seems like you have most of it down, so you can use them as a reference if you like. The third video (using custom data) is most pertinent to the topic of this post. Here are all three: