繁体   English   中英

Shopify 液体 - 无法操作部分内部的字符串

[英]Shopify Liquid - Unable to manipluate string inside section

所以我有一些数据是一个字符串(但我稍后会拆分成一个数组),我使用以下方法将它传递到一个部分:

{%- liquid
    assign myData = 'data],[data],[data'

    capture header
        section 'header'
    endcapture

    assign header = header | replace: '%%DATA%%', myData
-%}

然后在sections/header.liquid我有以下

{% capture replaced_data %}
%%DATA%%
{% endcapture %}

{%- liquid
    assign data = replaced_data | split: '],['

    echo data[0]
-%}

现在基于此,您希望打印出第一个data ,但实际上打印出以下数据

data],[data],[data

出于某种原因,一旦这些数据位于sections/header.liquid文件中,我就无法再对其进行任何操作。 我什至尝试在],[上进行替换,但它只是没有生效。 对此有什么想法吗?

对于包含所有内容的主文件theme.liquid 我的原始数据来自section 'categories.liquid ,然后通过一个片段,该片段只是对数据进行了一些整理,然后我在 header 中用它替换了%%DATA%% ,我也捕获了它。

{%- liquid
    capture categories
      section 'categories'
    endcapture

    capture header
      section 'header'
    endcapture

    capture category_data
      render 'category-data', data: categories, type: 'all'
    endcapture

    echo header | replace: '%%DATA%%', category_data
-%}

经过一些进一步的测试,似乎我能够将数据添加到字符串并删除它,但无法删除任何原始数据,例如这个例子:

assign data = replaced_data | append: '],[' | remove: '],['

这会在末尾添加],[并成功删除它,但不会删除字符串 rest 中的],[ 它几乎就像replaced_data 字符串是只读的

header output %%DATA%%因为assign data = replaced_data | split: '],[' assign data = replaced_data | split: '],['返回一个 0 的数组;

如果assign myData = 'data],[data],[data'assign header = header | replace: '%%DATA%%', myData assign header = header | replace: '%%DATA%%', myData header output data],[data],[data ,这是预期的。

修理,

{%- liquid
capture categories
  section 'categories'
endcapture

capture header
  section 'header'
endcapture

capture category_data
  render 'category-data', data: categories, type: 'all'
endcapture

 assign replacedHeader = header | replace: '%%DATA%%', 'data],[data],[data'
 assign headerArray = replaceHeader | split: "],[" 
-%}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM