theme-shopify-liquid-templates について
theme-shopify-liquid-templatesは、Liquidテンプレート、スニペット、ロジック、画像、SVGを扱うShopifyテーマ開発者向けの実践ガイドです。再利用可能なパターンにより、明確な構造とレスポンシブ画像の実装を支援します。
主な機能
- 01renderでスニペットを呼び出し使用例を記載
- 02liquidタグで複雑なロジックを整理
- 03image_tagでレスポンシブ画像を出力
- 04inline_asset_contentでSVGを読み込み
- 05深い条件分岐の回避
活用シーン
- 01Shopify Liquidテンプレートの作成・変更
- 02再利用可能なテーマスニペットの作成
- 03商品画像のレスポンシブなサイズ設定
- 04テーマテンプレートでのSVGアイコン利用
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ niccos-shopify-workspace/ shopify-cursor-skills --skill theme-shopify-liquid-templatesインストール後、Claude Code で/theme-shopify-liquid-templatesを入力して読み込まれたか確認します。
詳細
Shopify Liquid テンプレート
Shopify テーマにおける Liquid テンプレート、スニペット、ロジックフロー、画像の扱い、SVG の使用に関するベストプラクティスです。
使用する場面
- Liquid テンプレートを作成または変更する
- スニペットを扱う
- 画像やメディアを扱う
- Liquid ロジックを記述する
- SVG アイコンを使用する
スニペット
使用方法
{% render %}のみを使用します(includeは使用しないでください)- 各スニペットの先頭に 使用方法 ブロックを追加します
スニペットの構成
{%- comment -%}
Usage:
{% render 'snippet-name', param: value, another_param: value %}
{%- endcomment -%}
<div class="snippet-name">
{{ param }}
</div>
スニペットのパラメーター
パラメーターを使ってスニペットにデータを渡します。
{% render 'product-card',
product: product,
show_price: true,
image_size: 'large' %}
include ではなく render を使用する理由
renderのほうがパフォーマンスに優れています- 変数のスコープがより適切です
- Shopify が推奨しています
Liquid ロジック
{% liquid %} タグを使用する
長いロジックや複雑なロジックには、{% liquid %} タグを使用します。
{% liquid
assign discount = product.compare_at_price | minus: product.price
assign discount_percent = discount | times: 100 | divided_by: product.compare_at_price
if discount_percent > 20
assign is_big_discount = true
endif
%}
ロジックのベストプラクティス
- 条件分岐を深くネストしないでください
- 読みやすく、直線的なロジックを優先します
- 必要に応じて、複雑なロジックを複数の
liquidブロックに分割します - 意味が分かりやすい変数名を使用します
例:明快なロジックフロー
{% liquid
if product.available
assign button_text = 'Add to Cart'
assign button_class = 'btn--primary'
else
assign button_text = 'Sold Out'
assign button_class = 'btn--disabled'
endif
%}
<button class="{{ button_class }}">
{{ button_text }}
</button>
画像
常に image_tag を使用する
- 常に
image_tagフィルターを使用します - レスポンシブな
srcsetとサイズを使用します <img>タグをハードコードしないでください- 画像の URL を生成するには
<image_url>を使用します。 <image_url>には、必ず幅または高さのパラメーターを指定してください。
画像タグの構文
{{ image | image_url: width: image.width | image_tag: widths: '360, 720, 1080', loading: 'lazy' }}
レスポンシブ画像
{% assign image_widths = '360, 540, 720, 900, 1080, 1296, 1512, 1728, 1944, 2160' %}
{{ product.featured_image | image_url: width: product.featured_image.width | image_tag:
widths: image_widths,
sizes: '(min-width: 1200px) 50vw, 100vw',
loading: 'lazy',
alt: product.featured_image.alt | escape }}
画像の設定
image_tag の一般的なパラメーター:
widths:srcset 用の幅をカンマ区切りで指定したリストsizes:レスポンシブ画像用の sizes 属性loading:'lazy'または'eager'alt:代替テキスト(| escapeフィルターを使用)
SVG ファイル
インライン SVG
inline_asset_content フィルターを使用して SVG をインライン化します。
{{ 'icon-arrow.svg' | inline_asset_content }}
生の SVG を貼り付けない
- テンプレートに SVG マークアップをそのまま貼り付けないでください
- SVG ファイルは
assets/ディレクトリに保存します inline_asset_contentフィルターを使用して SVG を読み込みます
SVG の例
<button class="icon-button">
{{ 'icon-close.svg' | inline_asset_content }}
<span class="visually-hidden">Close</span>
</button>
SVG のスタイル設定
SVG はインライン化すると CSS でスタイルを設定できます。
.icon-button svg {
width: 24px;
height: 24px;
fill: currentColor;
}
Shopify テーマのドキュメント
以下の Shopify 公式リソースを参照してください。
よく使う Liquid パターン
商品カードのスニペット
{%- comment -%}
Usage:
{% render 'product-card', product: product, show_vendor: false %}
{%- endcomment -%}
<div class="product-card">
<a href="{{ product.url }}">
{{ product.featured_image | image_tag: widths: '360, 720', loading: 'lazy' }}
<h3>{{ product.title }}</h3>
<p>{{ product.price | money }}</p>
</a>
</div>
条件付きレンダリング
{% liquid
if section.settings.show_title
assign title_visible = true
else
assign title_visible = false
endif
%}
{% if title_visible %}
<h2>{{ section.settings.title }}</h2>
{% endif %}
指示
- スニペットには
renderを使用し、includeは使用しないでください - すべてのスニペットに 使用方法のコメントを追加してください
- 複雑なロジックには
liquidタグを使用してください - 常に
image_tagを使用し、<img>をハードコードしないでください inline_asset_contentフィルターを使用して SVG をインライン化してください- ロジックは直線的に保ち、深いネストを避けてください
- 適切な srcset とサイズを使用して レスポンシブ画像を使用してください
コメント
Markdown 対応