For a bilingual website, i have yaml data files for 2 languages.

files example:

en_services.yml
fr_services.yml

Variable example in my page:

---
lang: en
---

I want to loop trough the file with the lang as the prefix, something like that:

{% for service in site.data.{{ page.lang }}_services %}

{% endfor %}

This doesn't work, is there a way I can do that?

By the way, I don't think I can add subfolders in the _data folder, right?

Thanks.

有帮助吗?

解决方案

While that doesn't work, if you are able to put them both in the same file (grouped under the appropriate language code) there is a solution.

This gist was for another example based on post authors, but your should be able to use the same setup using language codes instead of author names.

其他提示

You should use:

site.data[{{ page.lang }} + '_services']

data[i] has key/value entries for all the files in your _data directory, and passing the string in, like so (for /_data/book.yaml):

site.data['book'] 

...totally works and opens up the possibility of concatenating strings inside the brackets with whatever variable you want. :)

{% capture thefile %}{{ page.lang }}_services{% end capture %}
{% for service in site.data[thefile] %}
...
{% endfor %}

Should do the trick.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top