Reputation: 169
How can I check if the current page is the cart page in theme.liquid? I tried page.handle=='cart'
but it is not working.
Upvotes: 6
Views: 12228
Reputation: 190
This is another option:
{% if request.path == routes.cart_url %}
We are on the cart page.
{% else %}
We are on some other page.
{% endif %}
Upvotes: 4
Reputation: 2559
Neither answer is absolutely correct, including the accepted one.
The cart template as others may have alternates e.g. cart.fancy.liquid
which can be accessed via /cart?view=fancy
. In that case, the equality operator will not work as expected as template
variable will be equal to cart.fancy
.
The contains
operator is not the best option as well, as it will also be TRUE for product.cartridge.liquid
and similar templates.
Based on the above here's the better solution to use in your themes:
{%- assign templateBase = template | slice: 0, 4 -%}
{%- if templateBase == 'cart' -%}
...
{%- endif -%}
cart
sequence in their suffixes i.e. alternate views.Upvotes: 4
Reputation: 3635
Also we can check using:
{% if template contains 'cart' %}
...
{% endif %}
Upvotes: 0
Reputation: 11682
You don't need the handle
filter, you can just use:
{% if template == 'cart' %}
...
{% endif %}
Upvotes: 17
Reputation: 169
I found a solution for this.
using {{template|handle}}
so my code is
{% capture template_handle %}{{ template | handle }}{% endcapture %}
{% if template_handle == 'cart' %}
{% endif %}
Upvotes: 1