【问题标题】:Liquid (Shopify) - Append String is EmptyLiquid (Shopify) - 附加字符串为空
【发布时间】:2019-04-09 08:46:58
【问题描述】:

在 Shopify 中,我正在尝试在我的 shopify 商店中构建当前标签的字符串。

如果我在页面上:

mysite.com/collections/all/tag1+tagC+tag4

我需要能够将当前标签作为一个没有空格的完整字符串:

tag1+tagC+tag4

我的代码目前是这样的:

{% if current_tags %}
    {% assign current_filters = '' %}
    {% for tag in current_tags %}
      {% if forloop.last == false %}
        {{ current_filters | append: tag | handleize | append: '+'}}
      {% else %}
        {{ current_filters | append: tag | handleize}}
      {% endif%}
    {% endfor %}
{% endif %}

如果我那么输出

{{current_filters}}

我明白了

tag1+ tagC+ tag4

首先,如何在加号后没有空格的情况下获取此字符串?我试过使用 | strip 没有运气,也把我的代码放在 {%- -%}

其次,当我尝试将 current_filters 变量附加到另一个变量的末尾时,它是空白/空

{% assign current_collection = collection.handle %}
{% assign base_url = shop.url | append: '/collections/' | append: current_collection | append: '/' | append: current_filters %}

输出 base_url 只是返回

mysite.com/collections/all/

不是

mysite.com/collections/all/tag1+tagC+tag4

为什么当我只使用 {{current_filters}} 而不是 .. append: current_filters 时这会起作用

【问题讨论】:

    标签: string append shopify liquid


    【解决方案1】:

    我认为你混淆了液体的基本语法。

    {{ ... }} 仅用于输出数据/内容,不用于分配。

    所以当你说:

    {{ current_filters | append: tag | handleize | append: '+' }} 
    // Logic "" (empty value) "tag" (the tag) "+" (the string)
    

    您输出current_filters 的空值,但添加tag 和+ 的值。但最后你根本没有修改 current_filters 值。所以最后它仍然是一个空字符串。

    为了分配/修改值,你总是使用{% ... %},所以在你的情况下你应该修改这个代码:

    {{ current_filters | append: tag | handleize | append: '+'}} 
    

    到这个:

    {% assign current_filters = current_filters  | append: tag | handleize | append: '+' %}
    

    此外,您还有join 过滤器,它将使上述所有代码变得多余。

    您只需拨打{{ current_tags | join: '+' }} 即可。

    【讨论】:

    • 谢谢。我是 Shopify 的新手,所以从头开始学习。欣赏深入的回应,这现在是有道理的。有没有快速获取标签句柄的方法(即{{ current_tags | join: '+' }}中的tag | handleize
    • @odd_duck 一种快速的方法是使用像 {{ current_tags | join: '___' | handle | replace: "___", '+' }} 这样的一行,我们用 3 "" 将它们连接起来,我们处理整个字符串,然后我们替换 3 " i>” 加上“+”,你就有了句柄。
    猜你喜欢
    • 2019-05-13
    • 2015-01-27
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 2023-02-23
    • 2019-06-13
    • 2022-01-21
    相关资源
    最近更新 更多