首页 > 解决方案 > 如何使用{{a variable [inside] a variable}}?

问题描述

我是 Jekyll 和 Liquid 的新手,我正在尝试做一些小事情,但我不知道如何让它发挥作用。

基本上:

this.works[well]

(我认为)

但:

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

所以这就是我的问题:如何在“变量路径”中插入单个变量?

基本上我想调用来自不同语言的文本字符串,它们现在位于我的 _data 文件夹中。我想做的是类似于:

_data/en.yml:

hello: ‘Hello!’

页面前端:

lang: en

字符串必须出现的位置:

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

任何正确的方法来做到这一点?

知道我会有几十个这样的字符串,所以它应该尽可能紧凑。

标签: jekyllliquid

解决方案


好的,我想通了,它是:

site.data[page.lang].hello

我有一个后续问题,我也在包含中传递了这些字符串。

这不起作用,并给出语法错误:

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

所以我必须做这样的事情:

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

这非常冗长和愚蠢:我最终写了 3 次(!)我的变量名,并创建了很多重复的变量。

任何人都知道如何在不写我的字符串名称 3 次的情况下编写?谢谢!!

(更愚蠢的是,您可以在 include 的第一部分使用带有 [] 的变量,但不能在第二部分使用?

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

例如可以工作……)


推荐阅读