--- name: liquid-patterns description: Common Liquid code patterns for Shopify theme development. Use when writing Liquid templates, handling translations, product displays, or theme customizations. --- # Shopify Liquid Patterns ## Translation in Liquid ```liquid {% comment %} Use locale files for hardcoded text {% endcomment %} {{ 'products.benefits.lifetime_guarantee' | t }} {% comment %} With default fallback {% endcomment %} {{ 'products.benefits.lifetime_description' | t: default: 'Fallback text' }} {% comment %} Dynamic key translation {% endcomment %} {% assign key = 'products.' | append: product.type | append: '.title' %} {{ key | t }} ``` ## Product Display Patterns ### Product Card ```liquid
{% if product.featured_image %} {{ product.featured_image.alt | escape }} {% endif %}

{{ product.title }}

{% if product.compare_at_price > product.price %} {{ product.price | money }} {{ product.compare_at_price | money }} {% else %} {{ product.price | money }} {% endif %}
``` ### Variant Selector ```liquid {% for option in product.options_with_values %}
{% endfor %} ``` ## Image Handling ```liquid {% comment %} Responsive images with srcset {% endcomment %} {% assign image = product.featured_image %} {{ image.alt | escape }} ``` ## Collection Loops ```liquid {% paginate collection.products by 12 %}
{% for product in collection.products %} {% render 'product-card', product: product %} {% endfor %}
{% if paginate.pages > 1 %} {% render 'pagination', paginate: paginate %} {% endif %} {% endpaginate %} ``` ## Metafield Access ```liquid {% comment %} Product metafield {% endcomment %} {% if product.metafields.custom.badge_type %} {{ product.metafields.custom.badge_type.value }} {% endif %} {% comment %} Shop metafield {% endcomment %} {{ shop.metafields.global.announcement | metafield_tag }} ``` ## Conditional Rendering ```liquid {% comment %} Check for specific template {% endcomment %} {% if template.name == 'product' %} {% comment %} Product-specific code {% endcomment %} {% endif %} {% comment %} Check customer status {% endcomment %} {% if customer %} {{ 'account.welcome' | t: name: customer.first_name }} {% else %} {{ 'account.login_prompt' | t }} {% endif %} ``` ## Form Patterns ### Add to Cart Form ```liquid {% form 'product', product %}
{% endform %} ``` ## Section Schema ```liquid {% schema %} { "name": "Custom Section", "settings": [ { "type": "text", "id": "heading", "label": "Heading", "default": "Section Heading" }, { "type": "image_picker", "id": "image", "label": "Image" } ], "presets": [ { "name": "Custom Section" } ] } {% endschema %} ``` ## Swiss Military Terminology Always use: - ✅ "badge" (NOT "écusson") - ✅ "section" (NOT "peloton") ```liquid {% comment %} Correct {% endcomment %} {{ 'products.badge.title' | t }} {% comment %} Wrong - never use {% endcomment %} {{ 'products.ecusson.title' | t }} ```