Skip to main content
Version: 0.2.x

Component templates

Although components can be used directly within any liquid files in static/, often your component has a set structure that you would have to create and maintain at multiple locations in your theme. SALVO-TS supports defining a standard template for a component alongside its class, which you can then use elsewhere in this theme using a convenient preprocessor tag.

This has two benefits:

  • When the component's HTML/liquid changes, you now only have a single place to update it.
  • SALVO-TS will automatically validate that required props are passed into the template in Liquid.
tip

Compiler macros are available in component templates.

Creating component templates

Component templates are stored in a matching .liquid file, next to the component they apply to. The .ts and .liquid files for a component must use the same filename, except for the extensions.

src/components/MyComponent.liquid
html
<my-component
name="{{ name|json|escape }}"
>
Hello, <span>{{ name }}</span>
</my-component>
src/components/MyComponent.liquid
html
<my-component
name="{{ name|json|escape }}"
>
Hello, <span>{{ name }}</span>
</my-component>

You can render a component in any liquid file using the {! component !} compiler macro.

static/layout/theme.liquid
js
{! component 'ComponentName', prop1: value, prop2: value, prop3: value !}
static/layout/theme.liquid
js
{! component 'ComponentName', prop1: value, prop2: value, prop3: value !}
static/snippets/example.liquid
html
<div>
{! component 'MyComponent', name: "foobar" !}
</div>
static/snippets/example.liquid
html
<div>
{! component 'MyComponent', name: "foobar" !}
</div>
tip

Other compiler macros are also available.

Prop validation

If you don't include a required prop when rendering a component template, an error will be rendered on the page instead.

static/snippets/example.liquid
html
<div>
<!-- Missing attribute for required 'name' prop: -->
{! component 'MyComponent' !}
</div>
static/snippets/example.liquid
html
<div>
<!-- Missing attribute for required 'name' prop: -->
{! component 'MyComponent' !}
</div>
Optional props

Props are required by default, but you can make a prop optional by passing a default value to the prop definition (i.e. @Prop({ default: 'not found' }) foo: string;).