【问题标题】:Liquid Template - Unable To Modify Jekyll Carousel ScriptLiquid 模板 - 无法修改 Jekyll Carousel 脚本
【发布时间】:2023-12-09 21:14:01
【问题描述】:

我正在使用 Jekyll 框架并使用他们网站 (https://jekyllcodex.org/without-plugin/slider/) 上的官方 Carousel 脚本,我正在努力弄清楚如何将名称传递给 carousel.html 脚本,以便我可以使用相同的脚本设置多个轮播。我的目标是能够使用:

 {% include carousel.html name="CatImages" height="20" unit="%" duration="7" %}

它应该能够简单地从 carousel.yml 文件中接收图像,类似于:`

CatImages: 
  - /assets/img/cat1.png
  - /assets/img/cat2.png

我尝试将字符串附加到变量名,这可行,但给我的输出略有不同——我无法弄清楚它的类型。结果变量来自

{{site.data.carousel.{{ include.name }}}}

返回 {"CatImages"=>["/assets/img/cat1.png", "/assets/img/cat2.png"]} 并且在运行 for 循环时,它将图像路径作为一个完整的字符串返回。例如,这个:

 {% for item in images %}
            {{item}}           
 {% endfor %}

返回:

images/assets/img/cat1.png/assets/img/cat2.png

但总的来说,我不明白这个变量是什么,也无法通过谷歌搜索找到它。我是 Liquid 模板的新手,但对于我的生活,我无法弄清楚 {""=>["",""]} 表示法是什么。

任何指针或提示将不胜感激!

【问题讨论】:

  • 如果在for 循环中调用{{ include.name }} 会发生什么?它应该返回“CatImages”。不是吗?
  • 确实如此!它输出“CatImages”,这很棒,这也是我能够获得输出{"CatImages"=>["/assets/img/cat1.png", "/assets/img/cat2.png"]} 的(奇怪)类型的方式。或许{{site.data.carousel.{{ include.name }}}}不是获取图片路径列表的正确方式?

标签: html templates jekyll liquid liquid-template


【解决方案1】:

我想通了!现在很明显,而且我在路上学到了很多东西(就像我实际上使用的是 Liquid 而不是 Django)。

我的问题是我试图这样做:

{{site.data.carousel.{{ include.name }}}}

但我所要做的只是:

{{site.data.carousel[include.name]}}

【讨论】: