这是代码:
{% for i in (0..5) %}
{% assign product = recommendations.top_related_products_{{i}}.products[0].title %}
{{ product }}
{% endfor %}
此代码引发错误 Liquid error: Cannot read property '0' of null
我有推荐下的6个数组,例如top_related_products_0...5
。
我如何打印在里面的标题变量top_related_products_0..5
?
创建一个包含变量名称的字符串,然后使用方括号表示法访问它。
例如:
{% for i in (0..5) %}
{% capture related_products %}top_related_products_{{i}}{% endcapture %}
{{ recommendations[related_products].products.first.title }}
{% endfor %}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句