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/ RPC and LiveTable in Templates

RPC and LiveTable in Templates

elements man html/integration

On This Page

  • RPC + HTML
  • LiveTable + HTML

Calling an @rpc function from a template and rendering a LiveTable in a template.

RPC + HTML

Mark a function @rpc to call it from the browser. The compiler wires the network call.

Server function
/** @rpc */ function saveUser(form: Form): User { return sql<User>(` insert into users ( name, age ) values ( ${form.name}, ${form.age} ) returning * `).firstOrThrow("insert returned no row"); }

Call it like a normal function from a handler.

Template
<form onsubmit={() => saveUser(form)}> <input value={form.name}> <input type="number" value={form.age}> </form>

These Elements functions are sync-style, which means you write them without await: sql, tx, calls to @rpc functions, session.login, channel.notify, LiveTable mutations and the others listed in man async. The compiler converts them to async at build time and propagates the conversion up the call stack. If you want the async version directly, import the xAsync variant (sqlAsync, txAsync, insertAsync).

Sync-style calls
let user = saveUser(form); let users = sql<User>(`select * from users`);

When you genuinely need explicit async (third-party libraries like the Stripe SDK, fetch, navigator.clipboard), declare the function async and await those specific calls. The build leaves manual async/await alone.

LiveTable + HTML

LiveTable provides crud and realtime for any reactive data source. The source can be a database table, a view, or anything else you can read and write. By default a LiveTable binds to a database table. Override its crud handlers to read and write another source.

Declaration
let comments = new LiveTable<Comment>();

This declaration binds to the comments database table. The table name is the variable name, converted from camelCase to snake_case. A write to comments from any browser updates every page that renders a view of comments.

In the template, iterate with e:for:

Template
<ul> <li e:for={comment of comments}>{comment.text}</li> </ul>

Mutate from event handlers. insert, update, and delete take an optional resetUI callback whose only job is to clear local UI state (form fields, drafts, edit toggles) after the optimistic write. Use it to reset UI, nothing else. Put navigation like redirect() in the handler, on the line after the mutation, not in resetUI:

Mutations
comments.insert({ text: form.text }, () => form.text = ""); comments.update({ ...comment, text: editText, }, () => editing = false); comments.delete(comment);

All three are sync-style with optimistic UI. The browser updates immediately. The server then writes the row and broadcasts it. On failure, the optimistic change rolls back.

e:for over a LiveView patches only the rows that changed. A row received from the server is one DOM insertion; an edit to a row repaints the bindings that read the edited field and nothing else.

Optimistic inserts must include every field the template displays. The optimistic row holds only the object you pass. If the template shows comment.author, insert({ text }) will render undefined for author until the server reconciles. Include displayed fields at insert time even if the server canonicalizes them. Server-generated columns the template reads count too: a displayed createdAt (default now()) is undefined on the optimistic row, so new Date(undefined) renders Invalid Date until the server's broadcast of the stored row arrives. Pass a client-side placeholder like createdAt: new Date().

Full LiveTable details (partitions, windows, custom select, custom handlers): livetable.

Prev Server RenderingNext Assets
Product
Manual Pricing Compare
Company
History Contact
Legal
Privacy Policy Acceptable Use Terms License Agreement
© Elements Dev Corporation 2026