Types

Types

Liquid objects can have one of six types:

You can initialize Liquid variables with the assign or capture tags.

String

Declare a string by wrapping a variable’s value in single or double quotes:

{% assign my_string = "Hello World!" %}

Number

Numbers include floats and integers:

{% assign my_int = 25 %}
{% assign my_float = 39.756 %}

Boolean

Booleans are either true or false. No quotations are necessary when declaring a boolean:

{% assign foo = true %}
{% assign bar = false %}

Nil

Nil is a special empty value that is returned when Liquid code has no results. It is not a string with the characters “nil”.

Nil is treated as false in the conditions of if blocks and other Liquid tags that check the truthfulness of a statement.

In the following example, if the user does not exist (that is, user returns nil), Liquid will not print the greeting:

{% if user %}
  Hello {{ user.name }}!
{% endif %}

Tags or outputs that return nil will not print anything to the page.

Input

The current user is {{ user.name }}

Output

The current user is

Array

Arrays hold lists of variables of any type.

Accessing items in arrays

To access all the items in an array, you can loop through each item in the array using an iteration tag.

Input

<!-- if site.users = "Tobi", "Laura", "Tetsuro", "Adam" -->
{% for user in site.users %}
  {{ user }}
{% endfor %}

Output

Tobi Laura Tetsuro Adam

Accessing specific items in arrays

You can use square bracket [ ] notation to access a specific item in an array. Array indexing starts at zero.

Input

<!-- if site.users = "Tobi", "Laura", "Tetsuro", "Adam" -->
{{ site.users[0] }}
{{ site.users[1] }}
{{ site.users[3] }}

Output

Tobi
Laura
Adam

Initializing arrays

You cannot initialize arrays using only Liquid.

You can, however, use the split filter to break a string into an array of substrings.

© 2005, 2006 Tobias Luetke
Licensed under the MIT License.
https://shopify.github.io/liquid/basics/types/

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部