Code blocks
Code blocks are the escape hatch at the far end of the block palette: when no built-in block and no designed card does what you need, write it. The code runs in a sandboxed frame, receives your data and the app's theme, and talks back through a small SDK that can only do what the template declares.
What a code block is
A code template holds three files, HTML, CSS and JavaScript, plus the variables it computes and the things it is allowed to do. Templates live on the Templates tab beside the custom-block and PDF templates. Each has a scope and, for the two data scopes, a table; both are fixed when the template is created. One template can back any number of blocks across views.
| Setting | Options | What it does |
|---|---|---|
| Code block, no data | scope: none | A content block, like text or image. The code gets the app, the viewer and the theme, plus variables over an empty row: a clock, a welcome panel, an animation, a calculator. |
| Code block on a record page | scope: record | Fills from the record the page is showing: its values and the template's variables evaluated against it. Can run that record's actions and update it. |
| Code list | scope: list | Handed the rows of a table, one frame for all of them, each row with its values and variables. Can write rows, run actions per row and open record pages. |
The sandbox, in plain words
Your code becomes the document of an iframe with sandbox="allow-scripts" and nothing else, loaded inline as srcdoc. That gives it an opaque origin: as far as the browser is concerned it belongs to no site at all.
- No cookies and no storage: the viewer's session never enters the frame.
- No access to the page around it:
window.parentis an opaque handle and the app's document is out of reach. - No network by default. A template may list the
httpshosts it needs (a chart library, a public API) and only those open up. - No top-level navigation, popups, form submission or modal dialogs.
- The app's theme arrives as CSS custom properties:
--primary,--card,--foreground,--border,--radius,--app-fontand the rest of the token set, so a code block can look native with no extra work. Dark mode is mirrored: the frame'shtmlelement carries thedarkclass whenever the app does.
Where the writes go
Variables
Variables are named formulas over the template's table, written in the same expression language as everything else: title = [Name] & " (" & [Status] & ")", pct = ROUND([Done] / [Total] * 100). They are row-scoped: evaluated against the one record on a record page, and once per row for a code list, then delivered as vars next to the raw values, so the code never parses dates or formats numbers itself.
A template with no data still has variables. They evaluate against an empty row with the viewer and app functions available: USEREMAIL(), USERROLE(), NOW(), APPNAME().
What the code receives
When the frame reports ready, the app sends one eb:init message. The SDK hands it to you as the result of Emberblocks.ready() and keeps the latest copy on the Emberblocks object; later changes arrive as eb:data, eb:theme and eb:viewport messages.
| Setting | Options | What it does |
|---|---|---|
| block | { id, title, scope } | The block hosting this frame. |
| app | { name } | The app's name. |
| user | { email, role } | The viewer, the same pair formulas see. Nothing else about the viewer crosses. |
| device | desktop / mobile | The device class the app is rendering for. |
| viewport | { width, height } | The frame's own size in CSS pixels; updated on resize. |
| theme | { dark } | Whether the app is in dark mode. The CSS variables already reflect it. |
| fields | [{ id, key, label, type, writable, options }] | The bound table's fields, hidden ones excluded: key is what values is indexed by, writable tells you which keys an update may carry, options lists select choices with their colours. |
| actions | [{ id, name, destructive }] | The actions the template declared and the viewer's role may run. Empty under a public link. |
| capabilities | { update, create, delete } | Which writes the template declared and this viewer may perform. |
| links | [{ viewId, name, kind }] | The views the template may open; kind is page, record or form. |
| readOnly | true / false | True under a public link: no actions, no writes. |
| record | { id, values, vars } | Record scope only: the record on the page, its raw values keyed by field key, and the evaluated variables. |
| rows | [{ id, values, vars }] | List scope only: the rows loaded so far, each with its values and variables. |
| hasMore | true / false | List scope only: whether another page exists for loadMore(). |
What the code can do
The frame's preamble defines exactly one global, window.Emberblocks, and nothing else. Calls that ask the app for something return a promise that settles when the app answers and rejects with the app's reason when it refuses.
| Setting | Options | What it does |
|---|---|---|
| ready() | Promise | Resolves with the init payload once the app has answered; immediately on later calls. |
| onData(cb) | unsubscribe | Called with { record, rows, vars, hasMore } on init and on every change. The natural place to render from. |
| onTheme(cb) | unsubscribe | Called with { dark } when the app toggles its theme. |
| onViewport(cb) | unsubscribe | Called with { width, height, device } when the frame's cell resizes or the device class changes. |
| runAction(actionId, rowId?) | Promise | Runs a declared action, against the given row on a list or the page's record otherwise. Confirmation prompts are the app's. |
| updateRow(rowId, patch) | Promise | Needs capabilities.update. patch is { fieldKey: value }; only writable keys are accepted. |
| createRow(values) | Promise<{ rowId }> | Needs capabilities.create. On a record page the record filter's prefill is merged in, as it is for forms. |
| deleteRow(rowId) | Promise | Needs capabilities.delete. The app asks the viewer to confirm first. |
| open(rowId) | Promise | Code list only: navigates to the record page set in Row click opens. |
| openView(viewId, rowId?) | Promise | Opens a declared view link; a record page needs a loaded rowId. |
| openUrl(url) | Promise | Opens an https URL in a new tab, from the app, with noopener. |
| loadMore() | Promise | Code list only: fetches the next page while hasMore is true. |
| resize(height?) | — | Asks the block to take this height, or the document's scroll height when omitted. Honoured only when the block's auto-height setting is on. |
| autoResize() | — | Watches the document and calls resize() for you; pass false to stop. |
| data, fields, actions, capabilities, links, readOnly | — | Read-only properties holding the latest payload, for code that renders outside onData. |
Declaring what the code may do
On the template's Capabilities tab you say, up front, what the code is allowed to ask for. The Templates tab shows the declaration as chips and the publish report lists it, so a widget written by Ember or pasted in cannot quietly gain a write path.
- Actions: a pick of the bound table's actions. Only these can be run.
- Capabilities:
update,createanddelete, each its own switch. - Links: the views the code may open; record pages are offered only when their table matches.
- Allowed hosts:
httpsorigins the frame may fetch from and load scripts, styles, fonts and media from. Empty means no network. - Allow eval: adds
unsafe-evalfor libraries that compile code at runtime. Leave it off unless a library refuses to load without it.
Public links
Under a public link a code block reads only. The init payload carries readOnly: true and an empty actions list, every write is refused, and the published bundle carries no capability or action declarations at all. Check Emberblocks.readOnly and hide the controls that would not work.
Limits
- 256 KB of code per template across the three files. Load libraries from an allowed host instead of pasting them in.
- 30 variables, 10 actions, 10 view links and 10 allowed hosts per template.
- 500 rows per code list block, in pages; the frame asks for more with
loadMore(). - Variables are row-scoped: no aggregates (
COUNT,SUMover other rows) and no lookups into related tables. Compute those in the code from the rows, or put a stat block beside the frame. - External libraries do not work offline: the frame has no cache of its own, so a code block that depends on a CDN shows nothing without a connection.
- Publish checks the template's structure (scope and table match the block, every variable parses, every declared action, link and host resolves) but it cannot run the code. Only the builder preview runs it: watch its console strip for errors before you publish.
A complete example
A list template over a Tasks table with one variable, title, and the update capability declared. The three tabs of the editor are shown in one listing.
<!-- HTML -->
<div id="cards"></div>
/* CSS */
.card { padding: 12px; margin-bottom: 8px; border: 1px solid var(--border);
border-radius: var(--radius); background: var(--card); }
.card h3 { margin: 0 0 6px; font-size: 14px; cursor: pointer; }
.card h3:hover { color: var(--primary); }
// JS
const cards = document.getElementById("cards");
Emberblocks.onData(({ rows }) => {
const canEdit = Emberblocks.capabilities.update;
cards.innerHTML = rows.map((row) => `
<article class="card" data-id="${row.id}">
<h3>${row.vars.title}</h3>
<label>
<input type="checkbox" ${row.values.done ? "checked" : ""} ${canEdit ? "" : "disabled"}>
Done
</label>
</article>`).join("");
});
cards.addEventListener("change", (e) => {
const id = e.target.closest("[data-id]").dataset.id;
Emberblocks.updateRow(id, { done: e.target.checked }).catch(console.error);
});
cards.addEventListener("click", (e) => {
if (e.target.tagName === "H3") Emberblocks.open(e.target.closest("[data-id]").dataset.id);
});
Emberblocks.autoResize();Set Row click opens on the block to the Tasks record page so open() has somewhere to go, and turn on the block's auto-height so autoResize() can grow it with its cards.