【问题标题】:Can I join the output of a loop with Liquid?我可以用 Liquid 加入循环的输出吗?
【发布时间】:2019-06-27 19:18:14
【问题描述】:

我希望输出如下内容: data-tags="[tag1, tag2, tag3]" 但相反,我收到了 data-tags:[tag1tag2tag3]。我是否错误地使用了加入?

代码:

data-tags="{% for tag in subtask.tags %}{{tag.title | slugify | join ', '}}{% endfor %}">

【问题讨论】:

    标签: html jekyll liquid


    【解决方案1】:

    试试这个:

    data-tags="{% for tag in subtask.tags %}{{ tag.title | slugify }}{% unless forloop.last %}, {% endunless %}{% endfor %}">

    如果你不需要slugify你可以做的标题:

    {% assign tags = subtask.tags | map: title %}
    <div data-tags="{{ tags | join: ', ' }}">
    

    这是因为连接过滤器只能应用于数组而不是数组的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-07
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 2016-10-18
      相关资源
      最近更新 更多