--- name: Shopify Snippet Library description: Reusable Liquid snippet patterns with proper documentation and parameter handling --- # Shopify Snippet Library ## Snippet Documentation Format All snippets should include documentation using Liquid comments: ```liquid {% comment %} Snippet: [Name] Description: [What this snippet does] Parameters: - param_name {type} - Description [required/optional] Usage: {% render 'snippet-name', param_name: value %} Example: {% render 'product-card', product: product, show_vendor: true %} {% endcomment %} ``` ## Common Snippet Patterns ### 1. Product Card ```liquid {% comment %} Snippet: Product Card Description: Displays a product with image, title, vendor, and price Parameters: - product {product} - The product object [required] - show_vendor {boolean} - Show product vendor (default: false) [optional] - image_size {number} - Image width in pixels (default: 400) [optional] - class {string} - Additional CSS classes [optional] Usage: {% render 'product-card', product: product, show_vendor: true, image_size: 600 %} {% endcomment %} {%- liquid assign show_vendor = show_vendor | default: false assign image_size = image_size | default: 400 -%} {%- if product == blank -%}

Product not available

{%- else -%}
{%- if product.featured_image -%}
{{ product.featured_image.alt | escape }}
{%- endif -%}
{%- if show_vendor and product.vendor != blank -%}

{{ product.vendor | escape }}

{%- endif -%}

{{ product.title | escape }}

{%- if product.compare_at_price > product.price -%} {{ product.price | money }} {{ product.compare_at_price | money }} {%- else -%} {{ product.price | money }} {%- endif -%}
{%- endif -%} ``` ### 2. Button / Link ```liquid {% comment %} Snippet: Button Description: Renders a customizable button or link Parameters: - text {string} - Button text [required] - url {string} - Button URL (makes it a link) [optional] - style {string} - Button style: 'primary', 'secondary', 'outline' (default: 'primary') [optional] - size {string} - Button size: 'small', 'medium', 'large' (default: 'medium') [optional] - class {string} - Additional CSS classes [optional] Usage: {% render 'button', text: 'Add to Cart', style: 'primary', size: 'large' %} {% render 'button', text: 'Learn More', url: '/pages/about', style: 'outline' %} {% endcomment %} {%- liquid assign style = style | default: 'primary' assign size = size | default: 'medium' assign btn_class = 'btn btn--' | append: style | append: ' btn--' | append: size if class assign btn_class = btn_class | append: ' ' | append: class endif -%} {%- if url != blank -%} {{ text | escape }} {%- else -%} {%- endif -%} ``` ### 3. Icon (SVG) ```liquid {% comment %} Snippet: Icon Description: Renders an SVG icon Parameters: - name {string} - Icon name (e.g., 'cart', 'heart', 'search') [required] - size {number} - Icon size in pixels (default: 24) [optional] - class {string} - Additional CSS classes [optional] Usage: {% render 'icon', name: 'cart', size: 20, class: 'icon-primary' %} {% endcomment %} {%- liquid assign size = size | default: 24 assign icon_class = 'icon icon-' | append: name if class assign icon_class = icon_class | append: ' ' | append: class endif -%} ``` ### 4. Form Input ```liquid {% comment %} Snippet: Form Input Description: Renders a form input field with label Parameters: - type {string} - Input type (text, email, tel, number, etc.) [required] - name {string} - Input name attribute [required] - label {string} - Input label [required] - required {boolean} - Make field required (default: false) [optional] - placeholder {string} - Placeholder text [optional] - value {string} - Default value [optional] - class {string} - Additional CSS classes [optional] Usage: {% render 'form-input', type: 'email', name: 'email', label: 'Email Address', required: true %} {% endcomment %} {%- liquid assign required = required | default: false assign input_id = 'input-' | append: name -%}
``` ### 5. Image with Fallback ```liquid {% comment %} Snippet: Responsive Image Description: Renders a responsive image with fallback Parameters: - image {image} - The image object [required] - width {number} - Image width (default: 800) [optional] - height {number} - Image height [optional] - alt {string} - Alt text (uses image.alt if not provided) [optional] - class {string} - Additional CSS classes [optional] - loading {string} - Loading attribute: 'lazy' or 'eager' (default: 'lazy') [optional] Usage: {% render 'image', image: product.featured_image, width: 600, alt: product.title %} {% endcomment %} {%- liquid assign width = width | default: 800 assign loading = loading | default: 'lazy' assign alt_text = alt | default: image.alt -%} {%- if image != blank -%} {%- if height -%} {{ alt_text | escape }} {%- else -%} {{ alt_text | escape }} {%- endif -%} {%- else -%} {%- endif -%} ``` ### 6. Price Display ```liquid {% comment %} Snippet: Price Description: Displays product price with sale indication Parameters: - product {product} - The product object [required] - show_compare {boolean} - Show compare at price (default: true) [optional] - class {string} - Additional CSS classes [optional] Usage: {% render 'price', product: product %} {% render 'price', product: product, show_compare: false %} {% endcomment %} {%- liquid assign show_compare = show_compare | default: true assign on_sale = false if product.compare_at_price > product.price assign on_sale = true endif -%}
{%- if on_sale -%} {{ product.price | money }} {%- if show_compare -%} {{ product.compare_at_price | money }} {%- endif -%} {%- else -%} {{ product.price | money }} {%- endif -%}
``` ### 7. Badge / Tag ```liquid {% comment %} Snippet: Badge Description: Displays a badge or tag Parameters: - text {string} - Badge text [required] - style {string} - Badge style: 'sale', 'new', 'featured', 'default' (default: 'default') [optional] - class {string} - Additional CSS classes [optional] Usage: {% render 'badge', text: 'Sale', style: 'sale' %} {% render 'badge', text: 'New Arrival', style: 'new' %} {% endcomment %} {%- liquid assign style = style | default: 'default' assign badge_class = 'badge badge--' | append: style if class assign badge_class = badge_class | append: ' ' | append: class endif -%} {%- if text != blank -%} {{ text | escape }} {%- endif -%} ``` ### 8. Loading Spinner ```liquid {% comment %} Snippet: Loading Spinner Description: Displays a loading spinner Parameters: - size {string} - Spinner size: 'small', 'medium', 'large' (default: 'medium') [optional] - class {string} - Additional CSS classes [optional] Usage: {% render 'loading-spinner', size: 'large' %} {% endcomment %} {%- liquid assign size = size | default: 'medium' assign spinner_class = 'spinner spinner--' | append: size if class assign spinner_class = spinner_class | append: ' ' | append: class endif -%}
Loading...
``` ## Best Practices ### 1. Always Document Parameters ```liquid {% comment %} Parameters: - param {type} - Description [required/optional] {% endcomment %} ``` ### 2. Provide Defaults ```liquid {%- liquid assign show_vendor = show_vendor | default: false assign image_size = image_size | default: 400 -%} ``` ### 3. Validate Required Parameters ```liquid {%- if product == blank -%}

Product parameter is required

{%- return -%} {%- endif -%} ``` ### 4. Handle Empty States ```liquid {%- if image != blank -%} {%- else -%}
No image available
{%- endif -%} ``` ### 5. Escape User-Provided Content ```liquid

{{ product.title | escape }}

{{ customer.name | escape }}

``` ### 6. Use Semantic HTML ```liquid
{%- # For products -%}