index.html
The main template file for your component
The index.html file is the primary template for your component. It defines the HTML structure, content areas, and markup that will be rendered for each instance of your component on a page.
Overview
When you create a new component, templates/index.html is typically the first (and often only) template file you'll need. It serves as the entry point for your component's visual output and contains the markup that defines how your component appears to users.
File Location
com.yourcompany.component/
├── templates/
│ └── index.html # Main component templateBasic Structure
A simple index.html might look like this:
<div class="{{classes.container}}">
@text("heading", title: "Heading", default: "Welcome")
@richtext("content", title: "Content", default: "Start typing...")
</div>Key Features
Property Insertion
Access component properties using double curly braces:
Properties can come from:
properties.json- User-defined propertieshooks.js- Data passed viarw.setProps()Built-in properties like
id,edit,preview
Content Areas
Define editable regions using content directives:
See the Elements Language documentation for all available directives.
Conditional Rendering
Show or hide content based on properties or modes:
Loops and Collections
Iterate over collections with @each:
Including Partials
Break complex templates into reusable pieces:
Include files are stored in templates/include/. See the Include Directory documentation.
Processing Behavior
Per-Instance Processing
The index.html template is processed once for each component instance on the page. If you add three instances of your component, the template runs three times with each instance's unique properties.
Output Location
The processed HTML from index.html is inserted directly where the component is placed in the page structure. It becomes part of the page's normal flow.
Common Patterns
Edit Mode Placeholders
Provide helpful UI when users are editing. Template @if accepts a single condition, so compute the combined check in hooks.js first. See Combining Conditions.
Responsive Classes
Use responsive property values for different breakpoints:
Properties can be configured as responsive in properties.json, and you can access breakpoint-specific values in hooks.js.
Component ID
Use the unique component ID for JavaScript targeting:
Alpine.js Integration
Integrate with Alpine.js for interactive components. Pass structured data through a data-* attribute rather than interpolating it directly into the x-data expression — see Data Attributes in the JavaScript Templates guide.
Do not interpolate plain JSON.stringify output into x-data (e.g. x-data="myComponent('{{id}}', {{options}})"). The JSON's double-quotes terminate the HTML attribute; Alpine fails to parse the expression and the component silently renders blank. Use a single-quoted data-* attribute as shown above, or the quote-swap pattern used by the core Table and Content Slider components.
Examples from Core Components
Simple Component (Typography)
The Typography component has a minimal index.html:
Interactive Component (Button)
The Button component uses conditionals to show different content areas:
Complex Component (Gallery)
The Gallery component combines conditionals, loops, and includes:
Best Practices
Keep It Focused
If your index.html becomes large and complex:
Extract repeated markup into includes
Move complex logic to
hooks.jsConsider breaking the component into smaller components
Use Semantic HTML
Structure your markup with proper HTML5 elements:
Provide Defaults
Always provide sensible default values for text areas:
This creates a better experience when users first add your component.
Consider Accessibility
Include proper ARIA attributes and semantic markup:
Related Documentation
Elements Language - Complete template syntax reference
Include Directory - Reusable template partials
Templates Overview - Understanding the templates directory
Properties - Defining component properties
Hooks.js - Passing data to templates
Last updated
Was this helpful?

