メインコンテンツへスキップ
Liquidは、OneSignalがメール、プッシュ、SMS、アプリ内、Live Activitiesを含むチャネル全体でメッセージをパーソナライズするためにサポートするテンプレート言語です。タグ、フィルター、条件文を使用してメッセージコンテンツを動的にカスタマイズします。 Liquid構文の詳細については、公式Liquidドキュメントを参照してください。

メッセージタイプ別のサポートされるフィールド

  • Email
  • Push
  • SMS
  • In-App Messages
  • Live Activities
  • 件名、返信先、プリヘッダー
  • メッセージ本文
  • HTMLブロックでの画像置換。例:<img src="{{image_url}}"/>
  • URL、メール送信などのボタンブロックアクションおよびその他のフィールド。

基本構文

Liquidは2つの主な構文構造を使用します:
  1. 出力タグ{{ ... }} - 変数またはオブジェクトからデータを表示します。
  2. ロジックタグ{% ... %} - 条件文またはループを実行します。

データソース

ソース説明
Tag{{ first_name }}OneSignal SDKまたはAPI(tags
Property{{ subscription.email }}システム管理(email、external_id、languageなど)
Journey Name{{ journey.name }}システム管理
Custom Data{{ message.custom_data.key }}Create Message APIで渡されるcustom_data
Data Feeds{{ data_feed.cart.size }}Data Feedsを使用すると、送信時にAPIからリアルタイムデータをメッセージに直接取り込むことができます。
Custom Events{{ journey.last_event.name }}Event Triggered JourneysWait Untilノードを介して設定

条件文

演算子

  • ==!=><>=<=
  • andor
  • contains(文字列または配列)
演算は右から左に実行されます。括弧はサポートされていません。
liquid
{% if true or false and false %}
  This is true.
{% endif %}

ifelsifelse

liquid
{%- assign userLang = subscription.language -%}
{% if userLang == "es" -%}
Hola {{ first_name }}!
{%- elsif userLang == "fr" -%}
Bonjour {{ first_name }}!
{%- else -%}
Hello {{ first_name }}!
{%- endif %}

unless

liquid
{% unless level == "1" %}
  Great job getting past the first level!
{% endunless %}

フィルター

{{ variable | filter }}を使用してフィルターを適用し、データの表示方法を調整します。

default

プロパティが空または存在しない場合にデフォルト値を割り当てます。
liquid
Hello {{ first_name | default: "there" }}.

date

dateフィルターは、タイムスタンプを別の日付形式に変換します。この構文の形式はstrftimeと同じです。入力はRubyのTime.parseと同じ形式を使用します。 タグを使用して日付をUnixタイムスタンプ(秒単位)として設定します。これにより、Liquid構文のパーソナライゼーションと時間演算子を使用したセグメンテーションの両方を使用できます。たとえば、タグは次のようになります:bill_due : 1687968776
liquid
{{ bill_due | date: "%a, %b %d, %y" }}
{{ "now" | date: "%Y-%m-%d %H:%M" }}

Result
Wed, Jun 28, 23
liquid
{{ bill_due | date: "%Y" }}
Result
2023
日付フォーマットは、適切にフォーマットされた日付が含まれている場合、文字列で機能します。
liquid
{{ "March 14, 2016" | date: "%b %d, %y" }}
Result
Mar 14, 16
現在の時刻を取得するには、dateフィルターと一緒に特別な単語now(またはtoday)を渡します。
liquid
This message was sent at {{ "now" | date: "%Y-%m-%d %H:%M" }}.
Result
This message was sent at 2022-11-02 14:39.
現在の時刻は、メッセージが受信者に送信されたときに基づいてメッセージにレンダリングされます。メッセージをテストしている場合、テストメッセージが送信されたときの現在の時刻が表示されます。

capitalize

このフィルターは、文字列の最初の文字を大文字にし、残りの文字を小文字に変換します。
liquid
{{ "my GREAT title" | capitalize }}
Result
My great title

round

このフィルターは、数値を最も近い整数に丸めます。引数として数値が渡された場合は、その小数点以下の桁数に丸めます。
liquid
{{ 1.2 | round }}
  {{ 2.7 | round }}
{{ 183.357 | round: 2 }}
Result
1
3
183.36

pluralize

このフィルターは、指定された数値に基づいて文字列の単数形または複数形を返します。数値は整数である必要があり、文字列として提供できます。文字列の単数形と複数形の両方を提供する必要があります。
liquid
{{ 1 | pluralize: "singular", "plural" }}
{{ 2 | pluralize: "singular", "plural" }}
1 {{ 1 | pluralize: "person", "people" }}
2 {{ 2 | pluralize: "person", "people" }}
2 {{ "2" | pluralize: "person", "people" }}
Result
singular
plural
1 person
2 people
2 people

イテレーション

forループ

コードブロックを繰り返し実行します。forループ内で使用可能な属性の完全なリストについては、forループオブジェクトを参照してください。
liquid
{% for product in message.custom_data.products %}
  - {{ product.name }}
  {% else %}
    Sorry, we're sold out of all products.
  {% endfor %}
Request Body
{
  "app_id": "5eb5a37e-b458-11e3-ac11-000c2940e62c",
    "template_id": "be4a8044-bbd6-11e4-a581-000c2940e62c",
    "custom_data": {
      "products": [
        { "name": "Sweater" },
        { "name": "Hat" },
        { "name": "Shirt" }
      ]
    }
  }
Results
  - Sweater
  - Hat
  - Shirt

  // if message.custom_data.products is empty
  Sorry we're sold out of all products
強力で柔軟性がありますが、Liquid構文でのforループの使用は、特定のまれなケースで通知配信のパフォーマンスを低下させる可能性があります。forループの使用には注意してください。また、いくつかのプッシュチャネルフィールド(contentsheadingssubtitleapns_alerturl)ではforループの使用を防止していることに注意してください。

limitoffset

ループを指定された反復回数に制限します。たとえば、メッセージに4つの製品のみを表示する場合、LimitsとOffsetsを使用して表示される製品の数を指定できます。
Data
great_numbers = [1,2,3,4,5,5]
liquid
{% for number in great_numbers limit:2 %}
  {{ number }}
{% endfor %}
Result
1 2
指定されたインデックスでループを開始するには、オフセット値を追加します:
Data
great_numbers = [1,2,3,4,5,5]
liquid
{% for number in great_numbers limit: 3 %}
  {{ number }}
{% endfor %}

{% for number in great_numbers limit: 3 offset: 3 %}
  {{ number }}
{% endfor %}
```text Result
1 2 3
4 5 6

where

指定されたプロパティ値を持つオブジェクトのみを含む配列を作成します。デフォルトでは、真の値を持つオブジェクトを含みます。 この例では、製品のリストがあり、キッチン製品を別々に表示したいと仮定します。whereを使用すると、typekitchenである製品のみを含む配列を作成できます。
Data
products = [
  {name:"Vacuum", type:"electronics"},
  {name:"Spatula", type:"kitchen"},
  {name:"Television", type:"electronics"},
  {name:"Garlic press", type:"kitchen"}
]
liquid
All products:
{% for product in products %}
- {{ product.name }}
{% endfor %}

{% assign kitchen_products = products | where: "type", "kitchen" %}

Kitchen products:
{% for product in kitchen_products %}
- {{ product.name }}
{% endfor %}
Result
All products:
  - Vacuum
  - Spatula
  - Television
  - Garlic press

Kitchen products:
- Spatula
- Garlic press

文字列操作

ユーザーに直接表示するのに適していない形式の文字列を含むデータタグがある場合があり、形式を調整するために文字列を操作する必要がある場合があります。以下は、文字列を調整するために使用できるLiquid構文コマンドの表です。タグとメッセージに直接書き込まれた文字列の両方で文字列操作を使用できます。
コマンド説明出力例
replace部分文字列を別の文字列に置き換えます。{{ 'hello world' | replace: 'world', 'there' }}hello there
capitalize文字列の最初の文字を大文字にします。{{ 'hello' | capitalize }}Hello
upcase文字列を大文字に変換します。{{ 'hello' | upcase }}HELLO
downcase文字列を小文字に変換します。{{ 'HELLO' | downcase }}hello
strip文字列から先頭と末尾の空白を削除します。{{ ' hello ' | strip }}hello
strip_html文字列からすべてのHTMLタグを削除します。{{ '<p>hello</p>' | strip_html }}hello
truncate文字列を指定された長さに短縮し、必要に応じて省略記号(…)を追加します。{{ 'This is a long sentence' | truncate: 10 }}This is a…
truncatewords指定された単語数の後で文字列を切り詰めます。{{ 'This is a long sentence' | truncatewords: 2 }}This is…
replace_first部分文字列の最初の出現を置き換えます。{{ 'hello world' | replace_first: 'world', 'there' }}hello there
prepend別の文字列の先頭に文字列を追加します。{{ 'world' | prepend: 'hello ' }}hello world
append文字列の末尾に文字列を追加します。{{ 'hello' | append: ' world' }}hello world
lstrip文字列から先頭の空白を削除します。{{ ' hello' | lstrip }}hello
rstrip文字列から末尾の空白を削除します。{{ 'hello ' | rstrip }}hello

FAQ

置換が機能しないのはなぜですか?

  • アプリ内メッセージはプロパティ置換をサポートしていません。
  • 「テストメッセージを送信」を使用する場合、タグ置換は機能しません。
  • タグキーは英数字である必要があります。または、_と-を使用します(ピリオドやスペースは使用できません)。
  • 置換はプレビューモードには表示されません。テストするには実際のメッセージを送信してください。

空白と改行を制御するには?

ハイフンを使用します:{{- ... -}}{%- ... -%}で周囲の空白をトリミングします。 詳細については、空白制御を参照してください。

ユーザー生成コンテンツを処理するには?

ユーザー生成テキストを{% raw %}{% endraw %}でラップして、Liquid解析を防止します。“raw”構文を参照してください。
json
{
  "contents": {
    "en": "{% raw %} Your user-generated content with invalid characters like {{ {% endraw %}"
  }
}