Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shopify 在液体模板中创建和访问阵列_Shopify_Liquid - Fatal编程技术网

Shopify 在液体模板中创建和访问阵列

Shopify 在液体模板中创建和访问阵列,shopify,liquid,Shopify,Liquid,他首先寻找一种方法来选择某些现有产品,以便在Shopify中的另一个页面(甚至在另一个产品页面中——因为一些产品被组合成其他产品)上放置标题、图像、描述 下面的方法似乎是我在Shopify中创建数组时遇到的唯一方法。(分割法) 等式的下一部分是使用{{myArray}}中的值来选择匹配的var,然后吐出存储在该数组中的不同值 然而,我的尝试没有成功。是否有方法向其他数组(即p1、p2、p3数组)添加键,以便在for循环期间更容易地选择它们 {% assign myArray = "p1|p3"

他首先寻找一种方法来选择某些现有产品,以便在Shopify中的另一个页面(甚至在另一个产品页面中——因为一些产品被组合成其他产品)上放置标题、图像、描述

下面的方法似乎是我在Shopify中创建数组时遇到的唯一方法。(分割法)

等式的下一部分是使用{{myArray}}中的值来选择匹配的var,然后吐出存储在该数组中的不同值

然而,我的尝试没有成功。是否有方法向其他数组(即p1、p2、p3数组)添加键,以便在for循环期间更容易地选择它们

{% assign myArray = "p1|p3" | split: "|" %}

{% assign p1 = "Product One|product-one|This is my description of product one, and it must be a single paragraphy without any html formatting. The length is not an issue.|product_one_image.jpg" | split:"|" %}

{% assign p2 = "Product Two|product-two|This is my description of product two, and it must be a single paragraphy without any html formatting.|product_two_image.jpg" | split:"|" %}

{% assign p3 = "Product Three|product-three|This is my description of product three, and it must be a single paragraphy without any html formatting.|product_three_image.jpg" | split:"|" %}

{% for item in myArray %}
    <h4>{{ item[0] }}</h4>
    <p>{{ item[2] }}</p>
{% endfor %}
{%assign myArray=“p1 | p3”| split:|“%”
{%assign p1=“Product One | Product One |这是我对Product One的描述,它必须是没有任何html格式的单个段落。长度不是问题。| Product_One_image.jpg”| split:|“%”
{%assign p2=“Product Two | Product Two |这是我对Product Two的描述,它必须是没有任何html格式的单个段落。| Product Two_image.jpg”| split:|“%”
{%assign p3=“Product Three | Product Three |这是我对Product Three的描述,它必须是没有任何html格式的单个段落。| Product_Three_image.jpg”| split:|“%”
{myArray%中项目的%s}
{{item[0]}
{{项目[2]}

{%endfor%}
一旦启动for循环,它将遍历数组中的每个值,因此索引(使用[])将不起作用,因为数组已经在迭代

在上面的示例中,您开始遍历列表,然后尝试为该项编制索引,这将不起作用。如果要为数组编制索引,则不要创建for循环,在上面的列表中,数组只有2项,但您选择了可用索引之外的索引,这是因为数组位置从0开始。因此,
标记应该是项[1],并且在for循环之外

做一个for循环。这样做:

{% for item in array %}
   <h4>{{ item }}</h4>
   {{ continue }}
   <p>{{ item }}</p>
{% endfor %}
{%用于数组%中的项]
{{item}}
{{继续}
{{item}}

{%endfor%}

continue标记将导致它迭代到for循环上的下一项。

连续性流在代码中是错误的

这是你需要做的

  • 将每个产品的产品元素加载到数组中

    {% capture list %}
        {% for product in products %}
        {{ product.title }}|{{ product.handle }}|{{ product.description}}|{{ product.featured_image }}{% if forloop.last %}^{% endif %}
        {% endfor %}
    {% endcapture %}
    {% assign p_list = list | split: "^" %}
    
  • 现在
    p_list
    将所有产品都包含为数组中的每个元素。现在是获得输出的时候了

    {% for p_item in p_list %}
        {% assign item = p_item | split: "|" %}
        <h4>{{ item[0] }}</h4>
        <p>{{ item[2] }}<p>
    {% endfor %}
    
    {%用于p_列表%中的p_项]
    {%assign item=p|u item | split:|“%}
    {{item[0]}
    {{item[2]}}
    {%endfor%}
    

  • 只是想让你知道,这已经帮助我解决了一个问题,我已经有相当一段时间了,谢谢!如果数组是字符串数组。假设我想为数组中的每个元素预先添加
    https://
    ,我该如何做?使用捕获修改数组中的元素并将其存储在新元素中。