【问题标题】:Shopify/liquid - adding classes and removing injected textShopify/liquid - 添加类和删除注入的文本
【发布时间】:2018-05-05 16:07:39
【问题描述】:

我正在开发一个经验非常有限的 Shopify (slate) 主题。我需要添加一些我在图像上管理过的类,但找不到在 <h2 class="mosaic__title p-name">{{ item.title | link_to: item.url }}</h2> 中包含的锚点上执行此操作的方法。

我还有一些价格,在它之前注入了诸如“On Sale from”之类的文字。我不知道这是如何添加或如何删除它的。这是一个例子:

<del>{{ item.compare_at_price | money }}</del>
{% assign sale_price = item.price | money %}
{{ 'products.product.on_sale_from_html' | t: price: sale_price }}

我已经尝试删除 .on_sale_from_htmlt: price: sale_price 但这不起作用/它坏了。

任何人都可以就此提出建议吗?谢谢!

完整部分代码供参考:

<div class="mosaic__caption">
  <h2 class="mosaic__title p-name">{{ item.title | link_to: item.url }}</h2>

  {% if item.object_type == 'product' %}
    <p class="mosaic__value">
      {% if item.compare_at_price > item.price %}
        {% if item.price_varies %}
          <del>{{ item.compare_at_price | money }}</del>
          {% assign sale_price = item.price | money %}
          {{ 'products.product.on_sale_from_html' | t: price: sale_price }}
        {% else %}
          {{ 'products.product.on_sale' | t }}
          <data itemprop="price" class="p-price">{{ item.price | money }}</data>
        {% endif %}
        <data class="visually-hidden p-price">{{ 'products.product.regular_price' | t }}</data>
      {% else %}
        {% if item.price_varies %}
          {% assign price = item.price | money %}
          <data itemprop="price" class="p-price">{{ 'products.product.from_text_html' | t: price: price }}</data>
        {% else %}
          <data itemprop="price" class="p-price">{{ item.price | money }}</data>
        {% endif %}
      {% endif %}
      {% unless item.available %}
      {{ 'products.product.sold_out' | t }}
      {% endunless %}
    </p>
  {% else %}
    <p>{{ item.content | strip_html | truncatewords: 50 }}</p>
  {% endif %}
</div>

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    让我们从link_to 过滤器开始。此代码:&lt;h2 class="mosaic__title p-name"&gt;{{ item.title | link_to: item.url }}&lt;/h2&gt;

    link_to 接受一个 URL 并使用提供的文本和链接创建一个 html 链接元素。

    所以上面的代码是一样的:

    <h2 class="mosaic__title p-name">
      <a href="{{item.url}}">{{item.title}}</a>
    </h2>
    

    因此您可以编写上述代码作为替代方案或使用替换过滤器添加类属性,例如:item.title | link_to: item.url | replace: '&lt;a', '&lt;a class="foo"'


    至于你的第二个问题,{{ 'products.product.on_sale_from_html' | t: price: sale_price }} 这样的输出指出这是一个可翻译的文本。

    这意味着您的文本位于您的翻译文件中(通常是 en.default.json 在您的 locales 文件夹中),因此您可以从那里修改文本。

    至于添加的文本,您翻译的字符串似乎包含以下{{ price }} 变量,该变量通过传递的变量price: sale_price 替换。

    PS:阅读 Shopify 中的文档,其中更详细地描述了这些功能: https://help.shopify.com/themes/liquid/filters/url-filters#link_to https://help.shopify.com/themes/development/internationalizing/locale-files#values

    【讨论】:

    • 感谢您的帮助,我对翻译文件有了更多了解。如果我根本不需要该文本,我可以将其从(例如){{ 'products.product.on_sale_from_html' | t: price: sale_price }} 代码中删除,还是应该从翻译文件中删除文本,所以将其留空?
    • 这取决于您,如果您不想要字符串,请删除文本。 (您也可以从管理面板编辑语言文件,在 Themes -&gt; More Actions -&gt; Languages 下)。
    • 是的,我根本不需要文本。我以为我可以删除 t 参考和 on_sale_form_html 并且它只会呈现价格,但看起来我不能这样做。
    猜你喜欢
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2022-12-18
    • 1970-01-01
    • 2020-10-01
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多