Using front matter variable to build a calling liquid object

I’m trying to take a page’s front matter, page.var and use it to append to a liquid object {{ site.data }}. I have a collection of data .yml's and I would like to define which one to use with front matter.

---
var: something
---

Then, in another file,

{{ site.data.something }}

Ive tried pre assigning things like,

{% assign data = site.data %}
{% assign something = page.var %}
{{ data.something }}

But that doesn’t work.

Also,

{% assign data = site.data | append: '.' | append: page.var %}
{{ data }}

I would rather not use any if's or similar conditionals as this would mean adding new conditionals every time I add a new var option.

OK I’ve come up with this… brace yourself its ugly!

{% assign page_var = page.var %}
{% for item in site.data %}
  {% assign file_name = item | split: '{' | first | remove: '"' | remove: '[' | remove: ',' | remove: ' ' %} 
  // This gets the file name without the .yml ext.
  {% if file_name == page_var %}
    {% assign data = item %}
  {% endif %}
{% endfor %}

Now {{ data }} is the appropriate .yml file according to the calling page’s var.

There must be a neater way of doing this…?

I think you could use square-bracket notation (untested code):

{{ site.data[page.var] }}
1 Like

:rofl: :man_facepalming: yeah ok, that does work!

Thank you.