简体   繁体   English

Liquid shopify中的数组操作

[英]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.

相关问题 如何在液体Shopify中进行插值 - How to interpolate in liquid shopify 如何将所有shopify过滤器添加到标准液体中 - How to add all the shopify filters to standard liquid JavaScript onerror在Shopify Liquid脚本中不起作用 - JavaScript onerror not working in Shopify Liquid script 在shopify片段目录中创建临时文件-Rails应用 - Create liquid files inside the shopify snippets directory - Rails app Shopify如何使他们的液体模板安全(避免使用XSS)? - How does Shopify make their liquid templates safe (avoid XSS)? 如何使用Liquid Shopify显示每张幻灯片3个项目的引导轮 - How to display bootstrap carousel with 3 items per slide using Liquid Shopify 如何在shopify液体中添加多个收藏夹以进行循环? - How can add multiple collection in shopify liquid for loop? 如何在液体shopify模板中隐藏自定义滑轨404页面上的页脚 - How to hide footer on customized rails 404 page in liquid shopify template 是否可以使用液体shopify读取本地文件内容并将其内容加载到页面中 - is it possible to read local files contents using liquid shopify and load its contents into the page 如何在我正在查看的页面上显示项目数? (在rails / shopify上使用液体/红宝石) - How I can display the number of items on the page I am looking at? (using liquid / ruby on rails / shopify)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM