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 template

Basic 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 properties

  • hooks.js - Data passed via rw.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:

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:

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:

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.js

  • Consider 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:

Last updated

Was this helpful?