--- name: Shopify Liquid Fundamentals description: Core Shopify Liquid templating best practices for performance, maintainability, and clean code --- # Shopify Liquid Fundamentals ## Core Principles ### Whitespace Control - Use `{%- -%}` to trim whitespace around Liquid tags - Prefer `{% render %}` over deprecated `{% include %}` - Use `{% liquid %}` for multi-line logic blocks **Example:** ```liquid {%- liquid assign product_available = product.available | default: false assign product_price = product.price | default: 0 if product == blank assign error_message = 'Product not found' endif -%} ``` ### Performance Optimization - Minimize Liquid logic in templates - Use `assign` for complex calculations instead of inline logic - Cache expensive operations - Use snippets for reusable code **Good:** ```liquid {%- assign discounted_price = product.price | times: 0.9 | money -%}
Sale: {{ discounted_price }}
``` **Bad:** ```liquidSale: {{ product.price | times: 0.9 | money }}
Save: {{ product.price | times: 0.1 | money }}
``` ### Error Handling Always provide defaults and handle empty states: ```liquid {%- liquid assign heading = section.settings.heading | default: 'Default Heading' assign show_vendor = section.settings.show_vendor | default: false if product == blank assign error_message = 'Product unavailable' endif -%} {%- if error_message -%}{{ error_message }}
{%- else -%} {%- endif -%} ``` ## Liquid Syntax Essentials ### Output ```liquid {{ variable }} # Output variable {{ variable | escape }} # Escape HTML {{ variable | default: 'fallback' }} # Provide default {{- variable -}} # Trim whitespace ``` ### Logic ```liquid {% if condition %} {% elsif other_condition %} {% else %} {% endif %} {% unless condition %} {% endunless %} {% case variable %} {% when 'value1' %} {% when 'value2' %} {% else %} {% endcase %} ``` ### Loops ```liquid {% for item in collection %} {{ item.title }} {% endfor %} {% for item in collection limit: 4 %} {{ item.title }} {% endfor %} {% for i in (1..5) %} Item {{ i }} {% endfor %} ``` ### Variables ```liquid {% assign name = 'value' %} {% capture variable %} Content here {% endcapture %} ``` ## Common Filters ### String Filters ```liquid {{ 'hello' | capitalize }} # Hello {{ 'HELLO' | downcase }} # hello {{ 'hello' | upcase }} # HELLO {{ 'hello world' | truncate: 8 }} # hello... {{ 'test
' | escape }} # <p>test</p> {{ 'hello world' | remove: 'world' }} # hello {{ 'hello' | append: ' world' }} # hello world ``` ### Array Filters ```liquid {{ collection | size }} # Number of items {{ collection | first }} # First item {{ collection | last }} # Last item {{ collection | join: ', ' }} # Join with comma {{ collection | reverse }} # Reverse order {{ collection | sort: 'title' }} # Sort by property ``` ### Math Filters ```liquid {{ 10 | plus: 5 }} # 15 {{ 10 | minus: 5 }} # 5 {{ 10 | times: 5 }} # 50 {{ 10 | divided_by: 5 }} # 2 {{ 10.5 | ceil }} # 11 {{ 10.5 | floor }} # 10 {{ 10.5 | round }} # 11 ``` ### Money Filters ```liquid {{ 1000 | money }} # $10.00 {{ 1000 | money_with_currency }} # $10.00 USD {{ 1000 | money_without_currency }} # 10.00 ``` ### Image Filters ```liquid {{ image | image_url: width: 400 }} {{ image | image_url: width: 400, height: 400 }} {{ image | image_tag }} {{ image | image_tag: alt: 'Description' }} ``` ## Shopify Objects ### Global Objects ```liquid {{ shop.name }} # Store name {{ shop.email }} # Store email {{ cart.item_count }} # Cart items {{ customer.name }} # Customer name (if logged in) {{ settings.color_primary }} # Theme setting ``` ### Product Objects ```liquid {{ product.title }} {{ product.price }} {{ product.compare_at_price }} {{ product.available }} {{ product.vendor }} {{ product.type }} {{ product.featured_image }} {{ product.url }} ``` ### Collection Objects ```liquid {{ collection.title }} {{ collection.description }} {{ collection.products }} {{ collection.products_count }} {{ collection.url }} ``` ## Best Practices ### 1. Always Escape User Input ```liquid{{ customer.name | escape }}
``` ### 2. Provide Defaults ```liquid {%- assign heading = section.settings.heading | default: 'Default Title' -%} {%- assign image = product.featured_image | default: blank -%} ``` ### 3. Check for Blank Values ```liquid {%- if heading != blank -%}No products available.
{%- endif -%} ``` ### Responsive Images ```liquid {%- if product.featured_image -%}{{ product.price | times: 1.1 | money }}
{{ product.price | times: 1.1 | money }}
``` ✅ **Do assign to variable:** ```liquid {%- assign price_with_tax = product.price | times: 1.1 | money -%}{{ price_with_tax }}
{{ price_with_tax }}
``` ❌ **Don't forget to escape:** ```liquid