Usage
To create a Component, add an HTML file incomponents:
components/alert.html
<yield /> tag will be replaced with the content passed to the Component:
emails/example.html
build_production/example.html
Includes
You can safely omit the<yield /> tag if you want to use Components as includes, and don’t actually need to pass any content to them:
components/alert.html
emails/example.html
build_production/example.html
Tags
There are two ways to use Components:- through the x-tag syntax
- through the
<component>tag
x-tag
Component names are automatically registered and can be used without having to specify their file path, by using a Blade-like syntax: start with the stringx- followed by the kebab-case name of the component file.
For example, let’s use the alert.html Component we created earlier:
emails/example.html
| Component file | x-tag syntax |
|---|---|
alert.html | <x-alert> |
alert_info.html | <x-alert_info> |
AlertInfo.html | <x-alertinfo> |
<component> tag
Alternatively, you may use the<component> tag to insert a Component:
emails/example.html
src attribute is mandatory and it needs to point to the Component’s file path, relative to the project root.
If you’re used to partials that you simply include in your HTML, this may look more familiar.
Nested file structure
If a Component is nested deeper within yourcomponents directory, you can reference it through dot notation.
For example, consider the following Component:
components/button/alt.html
emails/example.html
x-, and the file name comes after the dot.
Index files
If you add anindex.html Component inside a nested directory, you can also reference it without its file name part:
components/button/index.html
button/index.html Component:
emails/example.html
Slots
A Component may define slots, which act as placeholders that can be replaced (filled) with code when you use it. For example, let’s create a banner Component with a slot for a custom title:components/banner.html
emails/example.html
build_production/example.html
Default content
A slot may have default content, which will be used if it hasn’t been filled.components/banner.html
Prepend
You may prepend content to a slot by using theprepend attribute on the <fill> tag.
emails/example.html
slot:title example, that would result in:
build_production/example.html
Append
You may also append content to a slot by using theappend attribute on the <fill> tag.
emails/example.html
slot:title example, that would result in:
build_production/example.html
Check if a slot is filled
You may check if a slot has been filled by using the$slots variable in a Component.
For example, let’s create a <x-footer> Component that will pull in another Component based on whether a copyright slot has been filled or not:
components/footer.html
Discard default content
Sometimes you may need to discard the content of a slot that has default content. You can do this by using an empty<fill> tag.
Consider the following component:
components/banner.html
<fill> tag will will simply set the content to nothing:
emails/example.html
slot:title, resulting in:
build_production/example.html
Stacks
You may push content to named stacks that can be rendered in other Components. This concept is similar to Blade’sstack and push directives, also known as teleporting in frameworks like Vue.js.
For example, imagine you’re coding a Shopify email template and need to add some Liquid code at the very top of the HTML, before the doctype.
You would modify your Layout to include a stack tag:
layouts/layout.html
emails/example.html
build_production/example.html
once
You may use theonce attribute on the <push> tag to only push content once in a rendering cycle. This is useful if you’re rendering the Component in a loop and want to make sure the contents of <push> is only rendered once.
For example, imagine this Card Component:
components/card.html
head stack:
emails/example.html
Props
Props are attributes that can be added to a Component’s tag. They can be used to pass data to the Component, or to configure its behavior. To use props in a Component, you need to define them first. This is done by adding a<script> tag with the props attribute:
components/alert.html
<script> tag as the props object. In this example we’re getting the title prop from props.title, falling back to a default value if it’s not provided.
The script uses module.exports to export an object with props as keys. You can use these keys inside the Component through the curly braces syntax, as shown above.
To pass the title prop to the Component, you would use the title attribute:
emails/example.html
Reserved props
Thesrc prop is reserved when used on Components - it will always try to load a Component file at the path defined in the attribute value.
So if you’re trying to pass a src prop to a Component, you should use a different name:
components/alert.html
emails/example.html
src:
config.js
Encoding props data
When passing a props object to a Component, you need to encode the values. For example, these won’t work:Aware props
By default, props are scoped to the Component and are not available to nested Components. If you need to change this, you may use theaware: prefix when passing props to a Component:
Consider the following two Components:
components/child.html
components/parent.html
title to the x-parent Component:
emails/example.html
build_production/example.html
title prop was not passed down to the x-child Component, and it used the default value instead.
To make sure a prop is passed down to all nested Components, use the aware: prefix:
emails/example.html
build_production/example.html
Attributes
You may pass HTML attributes to a Component and they will be added to the root element of the Component. If you want to change the element to which the attributes are added, you can use theattributes attribute:
components/example.html
Expressions in attributes
Expressions may be used in a Component’s attribute:emails/example.html
Attribute removal
The following attributes will be removed from the target element:- unknown HTML attributes (see valid-attributes.js)
- attributes that are defined as props in the Component
Attribute merging
class and style attribute values will be merged with existing ones from the target element. All other attributes will be overwritten.
Safelist attributes
If you need to safelist or even block certain HTML attributes that you pass to a Component, see the Component configuration docs.Variables
When creating a Component, you have access to globalpage variables:
components/example.html
<x-example /> Component in a Template will render:
build_production/example.html