简体   繁体   English

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

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

So I have some data which is a string (but I'm going to split out later into an array), I'm passing it into a section with the following method:所以我有一些数据是一个字符串(但我稍后会拆分成一个数组),我使用以下方法将它传递到一个部分:

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

    capture header
        section 'header'
    endcapture

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

Then within sections/header.liquid I have the following然后在sections/header.liquid我有以下

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

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

    echo data[0]
-%}

Now based off that, you'd expect the first data to be printed out, but infact the following is printed out现在基于此,您希望打印出第一个data ,但实际上打印出以下数据

data],[data],[data

For some reason I'm no longer able to do any manipulation with this data once its in the sections/header.liquid file.出于某种原因,一旦这些数据位于sections/header.liquid文件中,我就无法再对其进行任何操作。 I've even tried doing a replace on the ],[ but it just doesn't take effect.我什至尝试在],[上进行替换,但它只是没有生效。 Any ideas on this?对此有什么想法吗?

For the main file theme.liquid which holds it all.对于包含所有内容的主文件theme.liquid My original data comes from the section 'categories.liquid , then passes through a snippet which just does some tidying up of the data and then I replaced the %%DATA%% with it in the header which I also captured.我的原始数据来自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
-%}

After some further testing, it seems I'm able to add data to the string and remove it, but unable to remove any of the original data, like this example:经过一些进一步的测试,似乎我能够将数据添加到字符串并删除它,但无法删除任何原始数据,例如这个例子:

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

This adds ],[ on the end and succesfully removes it, but it does not remove the ],[ in the rest of the string.这会在末尾添加],[并成功删除它,但不会删除字符串 rest 中的],[ Its almost like the replaced_data string is read only它几乎就像replaced_data 字符串是只读的

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

if assign myData = 'data],[data],[data' and assign header = header | replace: '%%DATA%%', myData如果assign myData = 'data],[data],[data'assign header = header | replace: '%%DATA%%', myData assign header = header | replace: '%%DATA%%', myData header output data],[data],[data , which is expected. assign header = header | replace: '%%DATA%%', myData header output data],[data],[data ,这是预期的。

to fix,修理,

{%- 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