我想拆分我的产品标题,例如:
"big hat - red"
"small coat - blue"
"medium socks - green"
使得它只输出"0"之前的文本-">。换句话说,我想要某种输出的动态拆分(或"拆分并删除余数"运算符/函数(
"big hat"
"small coat"
"medium socks"
使用下面的代码不会删除连字符后的文本,我也不能简单地对要删除的结尾部分进行硬编码,因为每个产品的连字符后面的文本都有所不同。
{{ product.title | split:"-" }}
我将如何实现这一点?
使用拆分过滤器,然后使用第一个过滤器来获得第一个数组元素。
{% assign source_string = "before text - after text"%}
{{ source_string | split: "-" | first }}
对于您的代码,它将是
{{ product.title | split:"-" | first }}