Template Syntax
On this page 17
stx uses an intuitive template syntax that extends HTML with additional features. This guide covers all the template syntax features available in stx.
Basic Syntax
Text Interpolation
Use double curly braces for text interpolation:
<div>{{ message }}</div>
Attributes
Bind attributes using the : shorthand:
<buttonx-disabled="">Submit</button>
Raw HTML
Use {{{ }}} for raw HTML interpolation (use with caution):
<div>{{{ rawHtml }}}</div>
Directives
stx accepts both Blade-style block directives and Vue-style attribute aliases. Use the style that best matches the template you are writing, but keep a single component consistent when possible.
Conditional Rendering
@if(user)
<p>Welcome, {{ user.name }}</p>
@else
<p>Welcome, guest</p>
@endif
<divif="">Shown if true</div>
<divelse-if="">Alternative</div>
<divelse>Fallback</div>
<divv-if="">Vue-style alias</div>
List Rendering
<ul>
@foreach(products as product)
<li>{{ product.name }}</li>
@endforeach
<lieach=""key="">
{{ item.name }}
</li>
<liv-for=""key="">
{{ item.name }}
</li>
</ul>
Event Handling
<buttonclick="">Click me</button>
<formsubmitprevent=""></form>
Two-way Binding
<inputmodel="">
<inputv-model="">
Server Data
Use <script server> for server-side data that should be evaluated during
rendering. Bare <script> and <script client> are preserved for the browser.
<scriptserver
export const products = [
{ name: 'Cedar Wick Candle', price: 28 },
{ name: 'Mineral Bath Salts', price: 18 },
]
</script>
@foreach(products as product)
<article>
<h2>{{ product.name }}</h2>
<p>${{ product.price }}</p>
</article>
@endforeach
Advanced Features
Dynamic Components
<componentis=""></component>
Slots
<templatedefaultDefaultcontenttemplate
<templatenamedNamedslotcontenttemplate
Custom Directives
<divcustom-directive=""></div>
Best Practices
- Always use key attributes with
@eachdirectives - Prefer
@ifover@showfor conditional rendering - Use computed properties for complex template expressions
- Keep template expressions simple and readable
TypeScript Support
stx templates fully support TypeScript, providing type checking for:
- Component props
- Event handlers
- Template expressions
- Directive arguments