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.
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.
- MyComponent.ts
- MyComponent.liquid
src/components/FooComponent.tstsimport {Component ,Prop ,Ref } from '@eastsideco/salvo-ts';import {ThemeComponent } from '@/theme/ThemeComponent';@Component export classMyComponent extendsThemeComponent {@Prop name !: string;}
src/components/FooComponent.tstsimport {Component ,Prop ,Ref } from '@eastsideco/salvo-ts';import {ThemeComponent } from '@/theme/ThemeComponent';@Component export classMyComponent extendsThemeComponent {@Prop name !: string;}
src/components/MyComponent.liquidhtml<my-componentname="{{ name|json|escape }}">Hello, <span>{{ name }}</span></my-component>
src/components/MyComponent.liquidhtml<my-componentname="{{ 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.liquidjs{! component 'ComponentName', prop1: value, prop2: value, prop3: value !}
static/layout/theme.liquidjs{! component 'ComponentName', prop1: value, prop2: value, prop3: value !}
- Source liquid
- Compiled liquid
- HTML output
static/snippets/example.liquidhtml<div>{! component 'MyComponent', name: "foobar" !}</div>
static/snippets/example.liquidhtml<div>{! component 'MyComponent', name: "foobar" !}</div>
dist/snippets/example.liquidhtml<div>{% render 'component_MyComponent', name: "foobar" %}</div>
dist/snippets/example.liquidhtml<div>{% render 'component_MyComponent', name: "foobar" %}</div>
HTMLhtml<div><my-componentname=""foobar"">Hello, <span>foobar</span></my-component></div>
HTMLhtml<div><my-componentname=""foobar"">Hello, <span>foobar</span></my-component></div>
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.
- Source liquid
- Compiled liquid
- Output HTML
static/snippets/example.liquidhtml<div><!-- Missing attribute for required 'name' prop: -->{! component 'MyComponent' !}</div>
static/snippets/example.liquidhtml<div><!-- Missing attribute for required 'name' prop: -->{! component 'MyComponent' !}</div>
static/snippets/example.liquidhtml<div><!-- Missing attribute for required 'name' prop: -->{% render 'component_MyComponent' %}</div>
static/snippets/example.liquidhtml<div><!-- Missing attribute for required 'name' prop: -->{% render 'component_MyComponent' %}</div>
HTMLhtml<div><!-- Missing attribute for required 'name' prop: --><div class="_SALVO_COMPONENT_ERROR">Required component prop 'name' not passed to MyComponent component template.</div></div>
HTMLhtml<div><!-- Missing attribute for required 'name' prop: --><div class="_SALVO_COMPONENT_ERROR">Required component prop 'name' not passed to MyComponent component template.</div></div>
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;).