【发布时间】:2015-03-17 04:13:24
【问题描述】:
如何检查当前页面是否是 theme.liquid 中的购物车页面?我试过page.handle=='cart',但它不起作用。
【问题讨论】:
标签: shopify
如何检查当前页面是否是 theme.liquid 中的购物车页面?我试过page.handle=='cart',但它不起作用。
【问题讨论】:
标签: shopify
您不需要handle 过滤器,只需使用:
{% if template == 'cart' %}
...
{% endif %}
【讨论】:
没有一个答案是绝对正确的,包括被接受的答案。
与其他人一样,购物车模板可能有替代品,例如cart.fancy.liquid 可以通过/cart?view=fancy 访问。在这种情况下,相等运算符将无法按预期工作,因为template 变量将等于cart.fancy。
contains 运算符也不是最佳选择,因为它对于product.cartridge.liquid 和类似模板也是TRUE。
基于以上内容,这是在您的主题中使用的更好解决方案:
{%- assign templateBase = template | slice: 0, 4 -%}
{%- if templateBase == 'cart' -%}
...
{%- endif -%}
cart 后缀中的序列,即备用视图。【讨论】:
这是另一种选择:
{% if request.path == routes.cart_url %}
We are on the cart page.
{% else %}
We are on some other page.
{% endif %}
【讨论】:
我找到了解决方案。
使用{{template|handle}}
所以我的代码是
{% capture template_handle %}{{ template | handle }}{% endcapture %}
{% if template_handle == 'cart' %}
{% endif %}
【讨论】:
我们也可以使用:
{% if template contains 'cart' %}
...
{% endif %}
【讨论】: