Push & Pop
The push and pop tags allow you to add/remove items in/from an array.
By default, Push & Pop work with the LIFO (Last In First Out) logic. For more information on the application of push and pop tags, please have a look at the case on the Silverfin Developer Community.
Push
Push tag will add an item at the end of your array.
{% push item to:array %}
Pop
Pop tag will remove the last item of your array and will give you the option to store this value into a variable.
{% pop array to:var %}
beginning attribute
It is possible to override the LIFO behaviour with the attribute "at:beginning" and insert/remove to/from the head rather than the tail of the array.
{% push item to:array at:beginning %}
{% pop array to:var at:beginning %}
Example
First you create an empty array, you can do this by splitting a string. You can split on any character, but we prefer pipes and semicolons. Then you push your items, here asset categories, into the array. If you then want to print all of the categories, you can print the array and join the items with a space so they're not stuck together.
{% assign asset_categories_array = "" | split:"|" %}
{% push "Land" to:asset_categories_array %}
{% push "Buildings" to:asset_categories_array %}
{% push "Furniture" to:asset_categories_array %}
{% push "Vehicles" to:asset_categories_array %}
{{ asset_categories_array | join:" " }}
And you can now take (pop) the last item and push it in a new array.
{% assign new_assets_categories_array = "" | split:"|" %}
{% pop asset_categories_array to:new_assets_categories_array %}
{{ asset_categories_array | join:" " }}
{{ new_assets_categories_array }}
Vehicles
Maximum array size
An array can hold a maximum of 5000 elements. Exceeding the limit will result in a liquid error.
Liquid error: push to 'an_array' failed as it has too many elements (> 5000). Last two elements are 'xxx, yyy' in code:
push \"yyy\" to:an_array
Updated over 1 year ago