How to Get Last Item of Array in Liquid in Shopify
Drive 20-40% of your revenue with Avada
Your online is encountering the inconvenience in showing items or product tags on your product page. If you are using Liquid template language, you can take a look to view these steps below to help you gather items. The tutorial aims to assist you in getting the last item of the array in Liquid.
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
In conclusion, adding items in one line may make the shopping journey become easier. Buyers will find it more convenient when shopping around because they do not need much time to seek key tags or information.Showing the first element as you want will help you provide important items to purchasers.