Developers

Site Code

Binding data to the designer

Send data from code to the designer with collections, take input from buttons and forms with actions, page data, formatters, conditions and window.anasaki

7 min readLast updated

View as Markdown

On this page

Developers declare data sources and actions in code, and designers use them in the designer without writing code — pages are rendered on the server (Google can read them, and they work with JavaScript off).

Collections: lists to display#

site.collection('events', {
  label: { th: 'อีเวนต์', en: 'Events' },
  fields: { title: 'text', date: 'date', image: 'image', body: 'html' },
  params: { category: 'text', q: 'text' },          // values from the designer/URL
  perPage: { default: 9, max: 30 },
  cache: 60,                                        // seconds
  access: ['*'],                                    // default ['administrator']
  load: ({ page, perPage, params }) =>
    db.table('events').where('category', params.category).paginate(page, perPage), // { items, total }
});

In the designer:

  1. Place a Collection and pick the data source from the "Site Code (site.collection)" group (a "Database tables" group reads a table directly, with columns, conditions and sorting)
  2. Design the first card — the others follow. Use {{item.title}} and {{item.date | date:'D MMM YYYY'}} in text
  3. Set the params as expressions such as query.category or 'news', and press Add pagination to this list to place a Pagination element (page numbers, previous/next or a load-more button)

If visitors don't see the list, check access in the code or Site Code → Access — a collection is administrators-only by default. The designer toolbar can switch View as visitor / me.

Actions: input from buttons and forms#

site.action('register', {
  input: { eventId: 'number', note: 'text?', email: 'email' },   // validated before run ('?' = optional)
  access: ['*'],
  run: ({ input, response }) => {
    db.table('registrations').insert({ event_id: input.eventId, email: input.email, note: input.note });
    return { message: 'Registered', refresh: ['events'] };       // show a message + re-render the collection
  },
});

In the designer, select any button or layer → interaction Call code (action) → choose the action → set the input (expressions, or "from a form field") → optionally Ask to confirm first, Re-render these layers and a success message.

run can return: message, redirect, refresh (collection names), update (layers), page (new values for {{page.*}}) — input errors answer 400 with per-field messages.

Forms: make a Frame a form and place Input elements (text, search, number, email, select, checkbox, textarea, hidden) inside. Inputs can also filter a list without an action.

Page data and formatters#

site.page('/checkout', { load: ({ request, user }) => ({ total: 1234.5 }) });  // {{page.total}}
site.formatter('baht', (v) => '฿' + Number(v).toFixed(2));                      // {{ item.price | baht }}

Built-in formatters: number[:decimals], date:'format' (Thai month names on Thai sites), upper, lower, truncate:n, default:v, json · plugin formatters are written <id>:<name>, e.g. {{ price | thai:baht }}

Expressions and conditions#

{{ … }} works anywhere data can be bound: paths (item.price, page.total, post.title, site.title, query.q, $index), the operators + - * / % < > == != && || ?? ?: and formatters (| number:2) — no function calls or [ ]. Every value is HTML-escaped.

Every layer has Condition → Show when: a falsy expression (false, null, '', 0, []) means the layer is never sent to the browser — the $empty condition designs how an empty list looks.

{{ item.stock > 0 ? 'In stock' : 'Sold out' }}
{{ item.price | number:2 }} THB
Show when: visitor.is.member

Page code and window.anasaki#

The Page code block (needs edit_scripts) has HTML, CSS (scoped to the block; :scope is the block itself) and JS (an ES module receiving el = the block and anasaki):

const r = await anasaki.call('register', { eventId: 5, email });    // call a site.action
if (!r.ok) el.querySelector('.err').textContent = r.message ?? '';
const stats = await anasaki.get('/api/stats', { days: 7 });         // call a site.route (same origin)
await anasaki.refresh('events');                                     // re-render a collection

JavaScript does not run on the designer canvas — press Try it in the preview to test.

Public actions are limited to 30 calls/minute/IP and need a CSRF token (the page runtime adds it). Never trust input without checks — always declare types in input.