【发布时间】:2021-12-15 23:14:03
【问题描述】:
我需要在 Shopify 的元字段中显示作为 URL 的四个随机图像之一,因此必须使用变量作为 img src。
{%- assign min = 1 -%}
{%- assign max = 4 -%}
{%- assign diff = max | minus: min -%}
{%- assign random_number = "now" | date: "%N" | modulo: diff | plus: min -%}
{% capture image %}
pages["explore-more-ingredients"].metafields.global.explore_ingredients_image{{ random_number}}.value
{% endcapture %}
因此,我使用该图像变量并查看以下各项在浏览器中的显示方式(附图片)
1. {{ image }} <br>
2. {{ pages["explore-more-ingredients"].metafields.global.explore_ingredients_image2.value }} <br>
3. <img src="{{ image }}">
4. <img src="{{ pages["explore-more-ingredients"].metafields.global.explore_ingredients_image2.value }}">
我做错了什么?由于 URL 是随机生成的,因此需要 3 号才能工作。这是语法问题吗?
当我查看如何在浏览器中呈现 3 的源代码时,它显示 <img src="pages[" explore-more-ingredients"].metafields.global.explore_ingredients_image3.value"="">
感谢您的指导。
【问题讨论】:
-
我有另一个解决这里语法错误的答案:stackoverflow.com/questions/70293919/… 简而言之,在使用 Liquid 时,我们从不将模板括号嵌套在模板括号内。你应该只需要
assign你的元字段键到一个变量,然后使用方括号表示法访问它。 :)