How to Get Last Item of Array in Liquid in Shopify
Drive 20-40% of your revenue with Avada
Getting the last item of an array in Shopify’s Liquid can be tricky without the right method. In this guide, we’ll show you a simple way to retrieve the last item, making your store management more efficient. Let’s get started!
Table of content
- Definition of Liquid template language?
- What is the array filter?
- How to get the last item of the array in Liquid
Definition of Liquid template language?
Liquid is used as the backbone of Shopify to upload dynamic content on the front page. Being one of the template language of Shopify, Liquid has been a popular source used by website developpers to create blogs or posts for the company.
Liquid code can be catehorized into objects, tags and filters. Objects annouce Liquid where to show content on a page. Objects and variable names are denoted by double curly braces {{ and }}. The main function of tags is to control the flow of the template. Tags are presented by one curly brace and a percent sign {% and %}. Filters are used to change the output of a Liquid object and seperated by a straight flash |
.
Liquid objects consist of five types: string, number, boolean, nil and array. The tutorial dig into the array filters and the way to show the last item of an array.
What is the array filter?
Array filter is a function used to create a new array from a provided array including names or items which meet the demand of the condition set by the argument function. In general, the formation of an array looks like this:
Filter feature is used to offer callback
function for each character in an array and build a new array that can also give the value back to confirm the true feature of that element. The callback
is compatible with three arguments: the value of the element, the index of the element and the array objects being traversed.
In addition, the range of elements demonstrated by the filter is set by the first petition of the callback. If existing elements are changed or eliminated, their value will be passed to the filter hence when filter check these elements, their value is kept.
How to get the last item of the array in Liquid
In the previous tutorial, we mentioned that the range of elements will be chosen randomly by the invocation of the callback. Therefore, in order to display the item exactly like your desire, you should follow these steps below.
Here is one simple formula that you can follow. For instance, you are selling clothes for men and women and you want to put the sales tag on these products, you can follow these steps below.
Input
<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags | last }}
Output:
awesome
If you use filter inside a tag, you can add first with dot notation:
{% if product.tags.last == "sale"%}
This product is on sale!
{% endif %}
If you want to show the last
letter or character of a chain, you use this code:
Input:
<!-- product.title = "Awesome Shoes" -->
{{ product.title | last }}
Output:
s
Conclusion
To sum up, getting the last item of an array in Liquid is crucial for ensuring that your Shopify store displays relevant and dynamic content effectively. By following the steps outlined in this guide, you can streamline the presentation of your product tags or other key elements, making the shopping experience smoother for your customers