Passing FrontMatter variable index of array to yml file in a data folder

Hi, guys

Thank you for reading this post.

I would like to pass Front Matter variable index to YAML file to let the Markdown file bring data corresponding to the index of yml file.

If you are okay, please give me some advice.

Front Matter

---
layout: archive
ref: [xl430-w210, xc430-w150, xh430-w210]
---

Markdown

| Item                    | Specifications                                                 |
|:------------------------|:---------------------------------------------------------------|
| MCU                     | {{ site.data.dxl_p_info[page.ref].mcu }}                 |

- [page.ref] refer to a yml file in data directory
- How should I put the index of  defined Front Matter variable into the liquid object and refer to yml file's index? 

Start with getting your output to work the plain way

| Item                    | Specifications                                                 |
|:------------------------|:---------------------------------------------------------------|
| ABC                     | {{ site.data.dxl_p_info['abc'] }}                 |
| XYZ                     | {{ site.data.dxl_p_info['xyz'] }}                 |

If that works then you can use a for loop.

{% for r in page.ref %}
- {{ r }} - value is {{ site.data.dxl_p_info['xyz'] }}
{% endfor %}

And if that works then you can make a table. I can’t tell though. Do you want one table with a row for each ref? Or a table for each ref, each with the same number of rows and same items in the first column

1 Like

I wrote this post on getting a data file to display as an HTML table (using markdown is tricky)

1 Like

Thank you for your advice,

I tried the given solution, and it works fine with using plain texts :slight_smile:

The table will have at least 10 rows with two columns. So I think i should just simply way to pass an parameter using Passing parameters to includes

Thank you so much for sharing your time (Also thank you for helping on other issued tickets as well)

Markdown with a front matter


---
layout: archive
lang: en
ref: xl430-w250
tab_ref:
  - ref1: xl430-w250
    ref2: xc430-w150
    ref3: xh430-w210
---

{% for reference in page.tab_ref %}

{% assign new_ref = reference %}

{% endfor %}

# [Specifications](#specifications)

{% include en/dxl/specifications_x.md passed_ref = new_ref.ref1 %}



Include fragment


{% assign ref_received = include.passed_ref %}

| Item                          | Specifications                                              |
| :---------------------------- | :---------------------------------------------------------- | 
| MCU                           | {{ site.data.dxl_x_info[ref_received].mcu }}                |
| Position Sensor               | {{ site.data.dxl_x_info[ref_received].encoder }}            |
| Motor                         | {{ site.data.dxl_x_info[ref_received]].motor }}             |
| Baud Rate                     | {{ site.data.dxl_x_info[ref_received].baudrate }}           |
| Control Algorithm             | {{ site.data.dxl_x_info[ref_received].control }}            |

I don’t know about this part. You’ve created an array with one element. So you have have to use [0] or a for loop to get to it

It translates to this using convert tool https://onlineyamltools.com/convert-yaml-to-json

{
  "tab_ref": [
    {
      "ref1": "xl430-w250",
      "ref2": "xc430-w150",
      "ref3": "xh430-w210"
    }
  ]
}

Unless you have more elements, I’d say just do

tab_ref:
  ref1: xl430-w250
  ref2: xc430-w150
  ref3: xh430-w210

i.e.

{
  "tab_ref": {
    "ref1": "xl430-w250",
    "ref2": "xc430-w150",
    "ref3": "xh430-w210"
  }
}
1 Like

Here’s my version.

---
layout: archive
lang: en
ref: xl430-w250
tab_ref:
  ref1: xl430-w250
  ref2: xc430-w150
  ref3: xh430-w210
---

## Specifications

{% include en/dxl/specifications_x.md passed_ref=tab_ref.ref1 %}
{% include en/dxl/specifications_x.md passed_ref=tab_ref.ref2 %}
{% include en/dxl/specifications_x.md passed_ref=tab_ref.ref3 %}

If you have ref20 for example or the number if not fixed, then a for loop would make sense as I mentioned before.

{% for ref in page.tab_ref %}
{% assign name = ref[0] %} <!-- e.g. ref1 -->
{% assign value = ref[1] %}  <!-- e.g. xl430-w250 -->

### {{ name }}

{% include en/dxl/specifications_x.md passed_ref=value %}
{% endfor %}

Don’t indent in the for loop or the markdown ### I added will turn into a codeblock.


And the includes file:

{% assign item_data = site.data.dxl_x_info[include.passed_ref] %}

| Item              | Specifications           |
| :---------------- | :----------------------- |
| MCU               | {{ item_data.mcu }}      |
| Position Sensor   | {{ item_data.encoder }}  |
| Motor             | {{ item_data.motor }}    |
| Baud Rate         | {{ item_data.baudrate }} |
| Control Algorithm | {{ item_data.control }}  |

Note there’s no need to store the value pass in to the includes. I did an extra variable to make your table simpler.

1 Like

Awesome,

This simple way {% assign item_data = site.data.dxl_x_info[include.passed_ref] %} really incredible to make my document readable !!

I always appreciate your supports :slight_smile:

I hope that you have best wishes and be safe during covid-19

1 Like