【问题标题】:Shopify Liquid - Unable to manipluate string inside sectionShopify Liquid - 无法在部分内操作字符串
【发布时间】:2022-01-06 14:55:48
【问题描述】:

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

{%- 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,然后通过一个sn-p,它只是对数据进行了一些整理,然后我在我也捕获的标题中用它替换了%%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: '],['

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

【问题讨论】:

  • 你怎么称呼这些文件?你在哪里调用 render/include/section ?
  • 刚刚用该信息更新了原始帖子

标签: shopify jekyll liquid


【解决方案1】:

标题输出%%DATA%%,因为assign data = replaced_data | split: '],['返回一个0数组;

如果assign myData = 'data],[data],[data'assign header = header | replace: '%%DATA%%', myData 标题输出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: "],[" 
-%}

【讨论】:

    猜你喜欢
    • 2019-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    相关资源
    最近更新 更多