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

font_modify フィルター(フォント装飾変更)

フォントオブジェクトのウェイト(太さ)またはスタイル(標準・イタリック・斜体)を変更する。指定したバリアントが存在しない場合は `nil` を返す。

用途
テーマ設定で選んだフォントをベースに、見出しはボールド、本文はノーマルウェイトといった使い分けが必要なとき。または、同じフォントファミリーの異なるウェイトやスタイルを動的に取得する場合。
設置場所
Liquid テンプレート内で `{{ font_object | font_modify: 'プロパティ', '値' }}` の形で使う。通常は CSS の `font-weight` や `font-style` プロパティに代入する変数に割り当てる。
注意点
フォントファミリーに指定したウェイト・スタイルのバリアントが存在しない場合、このフィルターは `nil` を返すため、`default` フィルターでフォールバック値を用意するか、出力前に `nil` チェックが必要。ウェイト指定では数値(100~900)、`normal`(400)、`bold`(700)、相対値(`+100` ~ `+900`、`-100` ~ `-900`)、`lighter`、`bolder` が利用可能。相対値で指定したとき、結果のウェイトが存在しないバリアントになる場合は CSS フォールバック規則に従う。
タグ:fontweightstylefiltercss

仕様

59 行 / json
{
  "category": "font",
  "deprecated": false,
  "deprecation_reason": "",
  "description": "The `font_modify` filter requires two parameters. The first indicates which property should be modified and the second is\neither the new value, or modification amount, for that property.\n\n> Tip:\n> You can access every variant of the chosen font's family by using [`font.variants`](/docs/api/liquid/objects/font#font-variants).\n> However, you can more easily access specific styles and weights by using the `font_modify` filter.\n\nThe following table outlines the valid font properties and modification values:\n\n<table>\n  <thead>\n    <th>Property</th>\n    <th>Modification value</th>\n    <th>Output</th>\n  </thead>\n  <tbody>\n    <tr>\n      <td rowspan=3><code>style</code></td>\n      <td><code>normal</code></td>\n      <td>Returns the normal variant of the same weight, if it exists.</td>\n    </tr>\n    <tr>\n      <td><code>italic</code></td>\n      <td>Returns the italic variant of the same weight, if it exists.</td>\n    </tr>\n    <tr>\n      <td><code>oblique</code></td>\n      <td>\n        <p>Returns the oblique variant of the same weight, if it exists.</p>\n        <p>Oblique variants are similar to italic variants in appearance. All Shopify fonts have only oblique or italic variants, not both.</p>\n      </td>\n    </tr>\n    <tr>\n      <td rowspan=7><code>weight</code></td>\n      <td><code>100</code> → <code>900</code></td>\n      <td>Returns a variant of the same style with the given weight, if it exists.</td>\n    </tr>\n    <tr>\n      <td><code>normal</code></td>\n      <td>Returns a variant of the same style with a weight of <code>400</code>, if it exists.</td>\n    </tr>\n    <tr>\n      <td><code>bold</code></td>\n      <td>Returns a variant of the same style with a weight of <code>700</code>, if it exists.</td>\n    </tr>\n    <tr>\n      <td><code>+100</code> → <code>+900</code></td>\n      <td>\n        <p>Returns a variant of the same style with a weight incremented by the given value, if it exists.</p>\n        <p>For example, if a font has a weight of <code>400</code>, then using <code>+100</code> would return the font with a weight of <code>500</code>.</p>\n      </td>\n    </tr>\n    <tr>\n      <td><code>-100</code> → <code>-900</code></td>\n      <td>\n        <p>Returns a variant of the same style with a weight decremented by the given value, if it exists.</p>\n        <p>For example, if a font has a weight of <code>400</code>, then using <code>-100</code> would return the font with a weight of <code>300</code>.</p>\n      </td>\n    </tr>\n    <tr>\n      <td><code>lighter</code></td>\n      <td>Returns a lighter variant of the same style by applying the rules used by the <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Meaning_of_relative_weights\">CSS <code>font-weight</code> property</a> and browser <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Fallback_weights\">fallback weights</a>, if it exists.</td>\n    </tr>\n    <tr>\n      <td><code>bolder</code></td>\n      <td>Returns a bolder variant of the same style by applying the rules used by the <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Meaning_of_relative_weights\">CSS <code>font-weight</code> property</a> and browser <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight#Fallback_weights\">fallback weights</a>, if it exists.</td>\n    </tr>\n  </tbody>\n</table>",
  "parameters": [
    {
      "description": "Font property to modify",
      "name": "property",
      "positional": true,
      "required": true,
      "types": [
        "string"
      ]
    },
    {
      "description": "The new value for the associated property.",
      "name": "value",
      "positional": true,
      "required": true,
      "types": [
        "string"
      ]
    }
  ],
  "return_type": [
    {
      "type": "font",
      "name": "",
      "description": "",
      "array_value": ""
    }
  ],
  "examples": [
    {
      "name": "",
      "description": "",
      "syntax": "",
      "path": "/",
      "raw_liquid": "{%- assign bold_font = settings.type_body_font | font_modify: 'weight', 'bold' -%}\n\nh2 {\n  font-weight: {{ bold_font.weight }};\n}",
      "parameter": false,
      "display_type": "text",
      "show_data_tab": false
    },
    {
      "name": "Non-existent font variants",
      "description": "If the `font_modify` filter tries to create a font variant that doesn't exist, then it returns `nil`. To handle this, you can either assign a fallback value with the [`default` filter](/docs/api/liquid/filters/default), or check for `nil` before using the variant.\n",
      "syntax": "",
      "path": "/",
      "raw_liquid": "{%- assign bold_font = settings.type_body_font | font_modify: 'weight', 'bold' -%}\n{%- assign italic_font = settings.type_body_font | font_modify: 'style', 'italic' -%}\n{%- assign heavy_font = settings.type_body_font | font_modify: 'weight', '900' | default: bold_font -%}\n{%- assign oblique_font = settings.type_body_font | font_modify: 'style', 'oblique' | default: italic_font -%}\n\nh2 {\n  font-style: {{ heavy_font.weight }};\n}\n\n.italic {\n  {% if oblique_font -%}\n    font-style: {{ oblique_font.style }};\n  {%- else -%}\n    font-style: {{ italic_font.style }};\n  {%- endif %}\n}",
      "parameter": false,
      "display_type": "text",
      "show_data_tab": false
    }
  ],
  "summary": "Modifies a specific property of a given font.",
  "syntax": "font | font_modify: string, string",
  "name": "font_modify"
}

出典・ライセンス

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