Liquid Snippets by ALSEL
📘 公式リファレンス🔧 リファレンス/フィルター初級

truncatewords フィルター(単語数で切詰め)

文字列を指定した単語数で切詰め、超過分に省略記号(...)を付加するフィルター。HTML を含む場合は事前に strip_html で除去する必要がある。

用途
ブログ記事の概要やプロダクト説明を一定の単語数で打ち切り、「続きを読む」へのリンクに繋ぐシーン。または検索結果やコレクション一覧で商品説明をプレビュー表示するとき。
設置場所
Liquid テンプレート内で `{{ article.content | strip_html | truncatewords: 15 }}` のように記述。第1引数に単語数、第2引数で省略記号をカスタマイズできる。
注意点
HTML タグは単語として数えられるため、HTML を含むコンテンツではこのフィルター適用前に必ず `strip_html` で除去する。省略記号を指定しない場合は `...` が自動付加され、不要な場合は第2引数に空文字 `''` を指定する。
タグ:stringtruncatefilterword-limit

仕様

40 行 / json
{
  "category": "string",
  "deprecated": false,
  "deprecation_reason": "",
  "description": "If the specified number of words is less than the number of words in the string, then an ellipsis (`...`) is appended to\nthe truncated string.\n\n> Caution:\n> HTML tags are treated as words, so you should strip any HTML from truncated content. If you don't strip HTML, then\n> closing HTML tags can be removed, which can result in unexpected behavior.",
  "parameters": [],
  "return_type": [
    {
      "type": "string",
      "name": "",
      "description": "",
      "array_value": ""
    }
  ],
  "examples": [
    {
      "name": "",
      "description": "",
      "syntax": "",
      "path": "/blogs/potion-notions/how-to-tell-if-you-have-run-out-of-invisibility-potion",
      "raw_liquid": "{{ article.content | strip_html | truncatewords: 15 }}",
      "parameter": false,
      "display_type": "text",
      "show_data_tab": true
    },
    {
      "name": "Specify a custom ellipsis",
      "description": "You can provide a second parameter to specify a custom ellipsis. If you don't want an ellipsis, then you can supply an empty string.\n",
      "syntax": "string | truncatewords: number, string",
      "path": "/blogs/potion-notions/how-to-tell-if-you-have-run-out-of-invisibility-potion",
      "raw_liquid": "{{ article.content | strip_html | truncatewords: 15, '--' }}\n\n{{ article.content | strip_html | truncatewords: 15, '' }}",
      "parameter": true,
      "display_type": "text",
      "show_data_tab": true
    }
  ],
  "summary": "Truncates a string down to a given number of words.",
  "syntax": "string | truncatewords: number",
  "name": "truncatewords"
}

出典・ライセンス

License:
MIT

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

関連項目

📘 公式リファレンス🔧 リファレンス/フィルター初級

avatar フィルター(顧客アバター画像HTML生成)

顧客アバターが存在する場合、それを表示する HTML タグを生成するフィルター。customer オブジェクトに対して使用する。

📁 theme-liquid-docs·MIT·19
📘 公式リファレンス🔧 リファレンス/フィルター初級

default_errors フィルター(フォーム送信エラーを日本語化)

form.errors の各エラーコードに対応する既定のエラーメッセージを自動生成するフィルター。フォーム送信時の入力値不正や必須項目空白などのエラーを言語別に出力する。

📁 theme-liquid-docs·MIT·19
📘 公式リファレンス🔧 リファレンス/フィルター初級

date フィルター(日付を指定形式でフォーマット)

日付値を指定されたフォーマット文字列に従って整形する。フォーマット文字列が空の場合は元の値がそのまま返される。入力が日付に変換できない場合も元の値が返される。

📁 theme-liquid-docs·MIT·19
📘 公式リファレンス🔧 リファレンス/フィルター初級

payment_terms フィルター(Shop Pay分割払いバナーを生成)

product フォームまたは cart フォームに対して使用し、Shop Pay の分割払いオプションを表示するための HTML を生成するフィルター。顧客が購入時に分割払いプランを選択できるようにする。

📁 theme-liquid-docs·MIT·19
📘 公式リファレンス🔧 リファレンス/フィルター初級

abs フィルター(数値の絶対値を取得)

負の数を正の数に変換する。数値の絶対値(ゼロからの距離)を返すフィルター。

📁 theme-liquid-docs·MIT·30
📘 公式リファレンス🔧 リファレンス/フィルター初級

append フィルター(文字列の末尾に追加)

文字列の末尾に別の文字列を連結するフィルター。URL パスの結合やテキストの接尾辞追加に使う。

📁 theme-liquid-docs·MIT·30