【问题标题】:Shopify / Liquid - split and delete after splitShopify / Liquid - 拆分后拆分和删除
【发布时间】:2021-02-02 07:35:03
【问题描述】:

我想拆分我的产品标题,例如:

"big hat - red"
"small coat - blue"
"medium socks - green"

使其只输出“-”之前的文本。换句话说,我想要某种输出的动态拆分(或“拆分并删除余数”运算符/函数)

"big hat"
"small coat"
"medium socks"

使用下面不会删除连字符后的文本,我不能简单地硬编码要删除的结尾部分,因为连字符后的文本因每个产品而异。

{{ product.title | split:"-" }}

我将如何实现这一目标?

【问题讨论】:

  • 我想知道是否有一个 excel 样式的选项来“查找”“-”并计算其中的字符数,作为可能的解决方案的一部分。但是,我在这方面找不到任何东西。

标签: shopify liquid


【解决方案1】:

使用split 过滤器,然后使用first 过滤器来获取第一个数组元素。

{% assign source_string = "before text - after text"%}
{{ source_string | split: "-" | first }}

对于您的代码,它将是

{{ product.title | split:"-" | first }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多