{{A variable [inside] a variable}}

Hi!

I’m new to Jekyll and Liquid and there is a little thing I’m trying to do and I can’t figure out how to make it work.

Basically:

this.works[well]

(I think)

But:

this.does.[not].work
this.does[not]work
[this].does.not.work
[this]does.not.work

So that is my question: how to insert an individual variable inside a “variable path”?

Basically I’d like to call text strings from different languages, that are in my _data folder for now.
What I would like to do is something akin to:

_data/en.yml:

hello: ‘Hello!’

page frontmatter:

lang: en

where the string has to appear:

{{ site.data[page.lang]hello }}

Any proper way to do this?
Knowing that I will have dozens of strings like this, so it should be as compact as possible.

Thanks!

a page variable is usually accessed like {{page.lang}} so maybe

{{ site.data.{{page.lang}}[hello] }}

I would first make sure you have the syntax correct - which I think is site.data.en[hello]

Ok I figured it out, it is:

site.data[page.lang].hello

I have a follow-up issue, it is that I also pass these strings in includes.

This doesn’t work, and gives a syntax error:

{% include box.html text=site.data[page.lang].hello %}

So I have to do things like this:

{% assign hello=site.data[page.lang].hello %}
{% include box.html text=hello %}

Which is extremely long and silly: I end up by writing 3 times (!) my variable name, and create a lot of duplicated variables.

Any one knows how to write without writing my string name 3 times?
Thanks!!

(Even more silly because you can use variables with [] in the first part of include, but not in the second one?

{% include site.data[page.lang].box text=hello %}

would work for instance…)