[英]Array Manipulation in Liquid shopify
I am trying to work out a conditional iteration in Liquid. 我正在尝试在Liquid中进行条件迭代。 This is what i have
这就是我所拥有的
{% capture title_tag %}
{% for teacher in course.teachers %}
{% if course.teachers.size == 1 %}
{{course.title}} with {{ teacher.name | escape }}
{% elsif course.teachers.size > 1 %}
{{ course.title }} with {{ teacher.name }}
{% endif %}
{% endfor %}
{% endcapture %}
As expected, the first 'if' condition works well and i get an output like this 不出所料,第一个“ if”条件运行良好,我得到了这样的输出
"Intro to Maths with Isaac Newton".
“艾萨克·牛顿的数学入门”。
My problem is with the elsif
, thus when the teacher size is greater than 1. I get this 我的问题是
elsif
,因此当教师人数大于1时,我得到了
"Intro to Maths with Isaac Newton Intro to Maths with Elon Musk".
“ Isaac Newton的数学入门”和“ Elon Musk的数学入门”。
What I actually want is 我真正想要的是
"Intro to Maths with Isaac Newton and Elon Musk"
“与艾萨克·牛顿和伊隆·马斯克的数学入门”
I would appreciate any help. 我将不胜感激任何帮助。 Thanks
谢谢
The issue is you want the course.title
to be printed not inside a loop . 问题是您要在不循环内打印
course.title
。
{% capture title_tag %}
{{ course.title }} with ⇐ !!!! HERE
{% for teacher in course.teachers %}
{% if course.teachers.size == 1 %}
{{ teacher.name | escape }}
{% elsif course.teachers.size > 1 %}
{{ teacher.name }}
{% endif %}
{% endfor %}
{% endcapture %}
joining the names with and
is more tricky and requires additional coding. 使用
and
将名称连在一起会比较棘手,并且需要附加编码。 Maybe you should just use String#join
: 也许您应该只使用
String#join
:
{% capture title_tag %}
{{ course.title }} with
{{ course.teachers.map { |t| t.name }.join(', ') }}
{% endcapture %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.