Hi! I have two .yml
files with the same structure contained in a folder into _data
folder named the same as a variable defined in a front matter of a page where I’d like to use one of them (see image below)
In Ruby language there is a very useful function called constantize
that evaluate a string as it would be part of the source code of the page. Does it exists a similar construct in Jekyll or Liquid? (I didn’t find it).
To be more clear this is my use case: starting from the image above I’v this code
{%- case page.title -%}
{%- when "branding" -%}
{%- for service in site.data.branding.services -%}
{% capture service_side_style %}{% cycle 'left', 'right' %}{% endcapture %}
{%- include service.html description=service align=service_side_style -%}
{%- endfor -%}
{%- when "ecommerce" -%}
{%- for service in site.data.ecommerce.services -%}
{% capture service_side_style %}{% cycle 'left', 'right' %}{% endcapture %}
{%- include service.html description=service align=service_side_style -%}
{%- endfor -%}
{%- else -%}
should not happen....
{%- endcase -%}
The code in every {% when %}
is almost the same and in the {% for %}
loop I use the same collection name as the case I’m in since I switch on the page name. Using a code like the one below (but it doesn’t work unfortunately ) would be the best since it would be short (no
when
required), dynamic (I can add as many folder with the same structure in the _data
directory without update the {% when %}
)
{% assign my_var=page.name | constantize %}
{% for service in site.data.my_var.services %}
Thank you very much!