Liquid Snippets by ALSEL
📘 公式リファレンス📦 リファレンス/オブジェクト初級

forloop オブジェクト

for ループの反復情報を持つオブジェクト。ループの進行状況(現在のインデックス、最初か最後か、全体の長さなど)にアクセスできる。

用途
リスト表示時にループの位置を判定して条件分岐したいとき。例えば「最後の項目以外はカンマで区切る」「最初の行は太字にする」といった処理で使う。
設置場所
for タグの内部で Liquid テンプレートに記述し、`{{ forloop.index }}`、`{{ forloop.first }}`、`{{ forloop.length }}` のようにプロパティを参照する。
注意点
forloop は for タグのスコープ内でのみ参照可能で、ループ外では undefined になる。ネストされたループの場合、`forloop.parentloop` で親ループの forloop オブジェクトにアクセスできるが、それ以上の祖先ループには直接アクセスできず、親を経由して参照する必要がある。index は 1 から始まり、index0 は 0 から始まるため、配列アクセス時は index0 を使う。
タグ:forlooploopiterationcontrol-flow

仕様

176 行 / json
{
  "access": {
    "global": false,
    "parents": [
      {
        "object": "forloop",
        "property": "parentloop"
      }
    ],
    "template": []
  },
  "deprecated": false,
  "deprecation_reason": "",
  "description": "",
  "properties": [
    {
      "deprecated": false,
      "deprecation_reason": "",
      "description": "",
      "examples": [],
      "return_type": [
        {
          "type": "number",
          "name": "",
          "description": "",
          "array_value": ""
        }
      ],
      "summary": "The total number of iterations in the loop.",
      "name": "length"
    },
    {
      "deprecated": false,
      "deprecation_reason": "",
      "description": "If the current `for` loop isn't nested inside another `for` loop, then `nil` is returned.",
      "examples": [
        {
          "name": "Use the `parentloop` property",
          "description": "",
          "syntax": "",
          "path": "/",
          "raw_liquid": "{% for i in (1..3) -%}\n  {% for j in (1..3) -%}\n    {{ forloop.parentloop.index }} - {{ forloop.index }}\n  {%- endfor %}\n{%- endfor %}",
          "parameter": false,
          "display_type": "text",
          "show_data_tab": true
        }
      ],
      "return_type": [
        {
          "type": "forloop",
          "name": "",
          "description": "",
          "array_value": ""
        }
      ],
      "summary": "The parent `forloop` object.",
      "name": "parentloop"
    },
    {
      "deprecated": false,
      "deprecation_reason": "",
      "description": "",
      "examples": [],
      "return_type": [
        {
          "type": "number",
          "name": "",
          "description": "",
          "array_value": ""
        }
      ],
      "summary": "The 1-based index of the current iteration.",
      "name": "index"
    },
    {
      "deprecated": false,
      "deprecation_reason": "",
      "description": "",
      "examples": [],
      "return_type": [
        {
          "type": "number",
          "name": "",
          "description": "",
          "array_value": ""
        }
      ],
      "summary": "The 0-based index of the current iteration.",
      "name": "index0"
    },
    {
      "deprecated": false,
      "deprecation_reason": "",
      "description": "",
      "examples": [],
      "return_type": [
        {
          "type": "number",
          "name": "",
          "description": "",
          "array_value": ""
        }
      ],
      "summary": "The 1-based index of the current iteration, in reverse order.",
      "name": "rindex"
    },
    {
      "deprecated": false,
      "deprecation_reason": "",
      "description": "",
      "examples": [],
      "return_type": [
        {
          "type": "number",
          "name": "",
          "description": "",
          "array_value": ""
        }
      ],
      "summary": "The 0-based index of the current iteration, in reverse order.",
      "name": "rindex0"
    },
    {
      "deprecated": false,
      "deprecation_reason": "",
      "description": "",
      "examples": [],
      "return_type": [
        {
          "type": "boolean",
          "name": "",
          "description": "",
          "array_value": ""
        }
      ],
      "summary": "Returns `true` if the current iteration is the first. Returns `false` if not.",
      "name": "first"
    },
    {
      "deprecated": false,
      "deprecation_reason": "",
      "description": "",
      "examples": [],
      "return_type": [
        {
          "type": "boolean",
          "name": "",
          "description": "",
          "array_value": ""
        }
      ],
      "summary": "Returns `true` if the current iteration is the last. Returns `false` if not.",
      "name": "last"
    }
  ],
  "summary": "Information about a parent [`for` loop](/docs/api/liquid/tags/for).",
  "name": "forloop",
  "examples": [
    {
      "name": "Use the `forloop` object",
      "description": "",
      "syntax": "",
      "path": "/",
      "raw_liquid": "{% for page in pages -%}\n  {%- if forloop.length > 0 -%}\n    {{ page.title }}{% unless forloop.last %}, {% endunless -%}\n  {%- endif -%}\n{% endfor %}",
      "parameter": false,
      "display_type": "text",
      "show_data_tab": true
    }
  ],
  "json_data": {
    "path": "",
    "handle": "",
    "data_from_file": "{\"first\":true,\"index\":1,\"index0\":0,\"last\":false,\"length\":4,\"rindex\":3}"
  },
  "return_type": []
}

出典・ライセンス

License:
MIT

このコードは Shopify 著作の MIT ライセンスソースです。 原本の著作権は Shopify が保有します。日本語訳は ALSEL によるものです。

関連項目

📘 公式リファレンス📦 リファレンス/オブジェクト初級

content_for_header オブジェクト

Shopify が必要とするスクリプト(分析・チェックアウト・言語設定など)をすべて動的に出力するオブジェクト。theme.liquid の <head> タグ内に埋め込む必須要素。

📁 theme-liquid-docs·MIT·20
📘 公式リファレンス📦 リファレンス/オブジェクト中級

metaobjects オブジェクト

ストア全体のメタオブジェクト定義にアクセスするグローバルオブジェクト。個別のメタオブジェクトはタイプとハンドルで参照でき、メタオブジェクト定義のエントリをループで反復処理できる。

📁 theme-liquid-docs·MIT·20
📘 公式リファレンス📦 リファレンス/オブジェクト初級

additional_checkout_buttons オブジェクト

PayPal Express Checkout など、オフサイト決済に対応した外部決済プロバイダーがストアに設定されているかを真偽値で返す。`content_for_additional_checkout_buttons` と組み合わせて、該当するチェックアウトボタンを条件付きで表示する。

📁 theme-liquid-docs·MIT·27
📘 公式リファレンス📦 リファレンス/オブジェクト初級

canonical_url オブジェクト

現在のページの正規 URL を取得するオブジェクト。Google などの検索エンジンに対してどのページ版が正規であるかを指定するために使用する。

📁 theme-liquid-docs·MIT·27
📘 公式リファレンス📦 リファレンス/オブジェクト初級

content_for_index オブジェクト

ホームページに表示するセクションの内容を動的に返すオブジェクト。Liquid インデックステンプレートで必ず使用する。

📁 theme-liquid-docs·MIT·27
📘 公式リファレンス📦 リファレンス/オブジェクト初級

content_for_additional_checkout_buttons オブジェクト

PayPal、Apple Pay、Google Pay など複数の決済プロバイダが有効になっているとき、その決済ボタンを HTML として出力するオブジェクト。`additional_checkout_buttons` で有無を判定したうえで、このオブジェクトで実際のボタンを表示する。

📁 theme-liquid-docs·MIT·27