0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Liquid】 フィルターとは

Last updated at Posted at 2025-02-11

フィルターとは

フィルターは、出力するStringの出力や変数を制御・変更するために仕様される。
フィルターを出力に適用するには、フィルタとフィルターを出力の中括弧の区切り文字 {{ }} の中に追加し、その前にパイプ文字 | を付ける。

使い方

以下の例だと、product がオブジェクト、title がプロパティ、upcase が適用されるフィルター。

Code
{% # product.title -> Health potion %}

{{ product.title | upcase }}
Data
{
  "product": {
    "title": "Health potion"
  }
}
Output
HEALTH POTION

フィルターとパラメーター

多くのフィルターはパラメーターを受け付け、フィルターの適用方法を指定することができる。
フィルタの中には、パラメータが有効であることを要求するものもある。

Code
{% # product.title -> Health potion %}

{{ product.title | remove: 'Health' }}
Data
{
  "product": {
    "title": "Health potion"
  }
}
Output
potion

複数のフィルターを使う

1つの出力に複数のフィルターを使用でき、左から右に適用される。

Code
{% # product.title -> Health potion %}

{{ product.title | upcase | remove: 'HEALTH' }}
Data
{
  "product": {
    "title": "Health potion"
  }
}
Output
POTION

今回はここまで。
Arrayフィルターなどはまた改めてまとめます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?