目前我正在使用 Jekyll 开发一个新网站,但遇到了一些问题。我有一个布局页面,我可以在其中使用每个页面的变量定义背景图像。
布局:
class="background background-{{ page.header_bg }}"
页面:
---
header_bg: storm
---
但现在我想根据变量值动态包含一些文件。好吧,我可以用一些 if 或 case 语句来做到这一点,但实际上我想做类似的事情
{% include page.header_bg %}
但这不起作用,因为 Jekyll 正在寻找一个名为“page.header_bg”的文件而不是值。
有人可以帮我吗?
最佳答案
根据to the docs ,您需要将变量名称放入附加 {{ }}
中。
引用链接:
ProTip™: Use variables as file name
The name of the file you wish to embed can be literal (as in the example above), or you can use a variable, using liquid-like variable syntax as in
{% include {{my_variable}} %}
.
关于include - Jekyll/Liquid 动态包含文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23541185/