elements
Start Docs Blog Demos UI Pricing
Elements Manual
  • Getting Started
  • CLI
  • Build
  • Browser
  • Packages
  • TypeScript
  • Elements HTML
    • Templates
    • Attributes
    • Directives
    • Events
    • Reactivity
    • Server Rendering
    • RPC and LiveTable in Templates
  • Assets
  • RPC
  • Router
  • Session
  • Database
  • Migrations
  • LiveTable
  • Channel
  • Jobs
  • Email
  • Payments
  • JSON
  • Async
  • Style
  • UI
  • JSOC
  • Config
  • Tests
  • Logs
  • Deploy
  • Editors
  • Recipes
  • Licenses
  • Issues
  • Changelog
Manual / Elements HTML/ Templates

Templates

elements man html/templates

On This Page

  • Files
  • Reusing a template
  • Templates
  • Slots

Where template files go, how a template is declared, and how a parent passes content into a child with slots.

Files

A page is a folder:

Directory layout
app/pages/home/ index.ts # route handler template.ehtml # templates, handlers, rpc, interfaces, LiveTables style.css # page styles test.ts

Everything the page needs starts in template.ehtml: its templates, event handlers, helpers, interfaces, @rpc functions, LiveTables, and Channels. index.ts and test.ts import from template.ehtml by name.

index.ts
import home, { posts, Post } from "./template"; // test.ts import { normalize, Post } from "./template";

An .ehtml file is a module like any other. The <html> template is its default export, other templates are named exports, and so is any TypeScript declaration you mark export.

Add a sibling services.ts when the markup gets hard to find in template.ehtml, and move the @rpc functions and interfaces there first. Split when the file is hard to read, not at a line count. Most pages stay in one file.

Move to app/shared/services/<topic>.ts when a second consumer appears: a second page, a job, an email, a route in app/routes/. One page plus one job counts; one page on its own does not. Shared templates go in app/shared/templates/.

Reusing a template

When two pages render the same UI with different values, write one template and use it in both pages. Sign in and sign up are the usual pair: the same field stack, the same error line, different labels and a different call on submit.

Terminal
elements create template auth-form -attrs='title: string, action: string'

That writes app/shared/templates/auth-form/{index.ehtml, style.css}, and both pages import it.

Extract when the shared part is most of the markup and the differences fit in attributes. Do not extract two elements that happen to look alike today, and do not add a variant attribute that switches between two layouts with nothing in common. Write a template whose body is one e:switch on its own attribute as separate templates, one per case.

A name with a folder creates the template in that folder: elements create template ui/button writes it under app/shared/templates/ui/. A template used by one page needs no command. Declare it as a second top-level tag in that page's template.ehtml.

Templates

A template name must start with a capital letter. <Card>, <FilterTab>, <AuthForm>. This is enforced, not a style preference: a call site reads a capitalized tag as a template and a lowercase one as a native HTML element, so a template declared <card> could never be called. Declaring one is an error, "The template name <card> must start with an uppercase letter."

<html> is the one exception, and a special reserved name. Marking a template as <html> makes it the default export of the file and turns it into an HTML page. Other named templates are named exports.

A page's CSS is every stylesheet reachable by import from its <html> template: the template's own imports, then each child template's imports, depth first. The @import lines are written in the CSS files, so a page's style.css is the file that imports app/shared/styles/page.css. A rule written in another page's style.css is not in that set, which means markup copied from one page does not bring that page's styles with it. Scripts are bundled the same way: every script reachable by import from the <html> template.

Only a top-level page's style.css, the stylesheet of the template that renders <html>, imports app/shared/styles/page.css. A shared template's style.css imports nothing: it uses tokens and classes directly, because the page already loaded them. Importing page.css again from a template applies the whole base a second time.

app/shared/templates/auth-form/style.css
.auth-form { display: grid; gap: var(--space-4); }
index.ts
import html from "./template"; import html, { Footer } from "./template"; // page + named template

Import a template extensionless: ./template resolves to template.ehtml.

Templates compose like function or class declarations: define them in one file or split across multiple files.

A template name is a type as well as a value, so it cannot be shared with a type declared beside it. An interface Item and an <Item> template in the same file are two declarations of Item, and the compiler reports a duplicate identifier on both. Give the data a different name from the template that renders it (Message and <MessageRow>, or ItemData and <Item>).

Slots

Slots are how a parent passes content into a child template. The child declares <slot/> placeholders; the parent fills them. The syntax is standard html.

Slots
<Panel> default content <footer slot="footer">footer content</footer> </Panel> <Panel> <slot/> <slot name="footer">default footer</slot> </Panel>
Prev Elements HTMLNext Attributes
Product
Manual Pricing Compare
Company
History Contact
Legal
Privacy Policy Acceptable Use Terms License Agreement
© Elements Dev Corporation 2026