Mustache Template Basics
Master the fundamentals of Mustache templating for Quickbutik themes
Mustache is a logic-less templating language that Quickbutik uses to create dynamic storefronts. This guide covers the core syntax and fundamental concepts you need to get started with Mustache templates.
Why Mustache?
- Simple, readable syntax
- Logic-less design prevents complex business logic in templates
- Cross-platform compatibility
- Secure by design - prevents code injection
Template Syntax Overview
All Mustache templates use double curly braces {{}}
to denote dynamic content. Here are the basic patterns:
Variables
{{variable}}
- Outputs the value of a variable
Sections
{{#section}}...{{/section}}
- Conditional blocks or loops
Inverted Sections
{{^section}}...{{/section}}
- Shows content when condition is false
Comments
{{! This is a comment }}
- Not rendered in output
Variables and Output
The simplest Mustache tag outputs a variable’s value:
HTML Escaping
By default, Mustache escapes HTML characters for security. Use {{{variable}}}
or {{&variable}}
to render unescaped HTML:
Security Note: Only use unescaped output ({{&}}
or {{{}}}
) with trusted content like product descriptions that you control. Never use it with user-generated content.
Basic Sections and Conditionals
Sections are the core of Mustache’s logic. They render content based on the truthiness of a value:
Basic Loops
When a section’s value is an array, Mustache loops through each item:
Basic Object Access
Use dot notation to access object properties:
Understanding Context
Inside a section, the context changes to that object:
Helper Functions Overview
Quickbutik provides special wrapper functions for common tasks:
Learn More: Helper functions are covered in detail in the Wrappers and Functions guide.
Best Practices
Keep It Simple
Mustache is designed to be logic-less. Keep business logic in your data, not your templates.
Use Descriptive Names
Use clear, descriptive variable names in your templates for better maintainability.
Handle Empty States
Always provide fallbacks for empty arrays and missing data.
Escape User Content
Use default (escaped) output for any user-generated content.
Template Organization
Common Mistakes to Avoid
Testing Your Templates
Use the preview function
Always test your templates using Quickbutik’s preview feature before publishing
Test edge cases
Test with products that have no images, are sold out, or have no variants
Check different page types
Make sure your templates work across different contexts (product pages, category pages, etc.)
Validate HTML output
Use browser developer tools to ensure your templates generate valid HTML
Next Steps
Now that you understand the basics, dive deeper into specific aspects:
Objects and Attributes
Learn about all the data objects and how to access their properties
Conditionals
Master advanced conditional logic and pattern matching
Wrappers and Functions
Discover all available helper functions and their usage
Global Objects
Explore store-wide data like settings, navigation, and cart