Basics
Liquid Data Types
Liquid Data Types
Liquid data types include strings numbers and arrays with dynamic typing.
Introduction to Liquid Data Types
Liquid, a powerful templating language used in platforms like Shopify, utilizes dynamic typing. This means variables can hold any data type and can change types as needed. Understanding the different data types in Liquid is crucial for effective template development.
Strings in Liquid
Strings in Liquid are sequences of characters, often used to represent text. Strings are enclosed in double quotes.
Here is an example of a string in Liquid:
You can concatenate strings using the | append
filter or by reassigning the variable with a new string.
Numbers in Liquid
Liquid supports numbers, which can be integers or floating-point numbers. Numbers are not enclosed in quotes, and you can perform arithmetic operations directly on them.
Here is an example of using numbers in Liquid:
In this example, the variable result
will hold the value 50
.
Arrays in Liquid
Arrays in Liquid are ordered lists of items that can be of any data type, including strings, numbers, or even other arrays. They are useful for storing collections of data.
Here's an example of an array:
This code splits a string into an array of fruits. You can access elements in an array using Liquid's iteration tags.
Here's how you can iterate over an array:
This loop will output each fruit in the fruits
array.
Dynamic Typing in Liquid
Dynamic typing in Liquid allows for flexibility in assigning and reassigning variables to different types. This means you can have a variable initially as a string, and later change it to a number or an array, depending on the needs of your template logic.
For instance:
In this example, the data
variable starts as a string but is used as a number in the second assignment, showcasing Liquid's dynamic typing.