Example: Simple Blog

A complete working blog on web pages: a list page, a post page with its own URL per post, an editor section with a publishing flow, live search and a JSON feed.

One post document holds everything a list shows

Post Document
{
  "_meta": {
    "cms": 1782080295147,
    "created": 1782080295,
    "entity": 1,
    "expiry": -1,
    "guid": "a23575f49d0af385314c1f02280163374297e018a692e5e4ab85eb307ebf6ebc",
    "removed": 0,
    "ums": 1782166695813,
    "update_hash": "60e27209fa93063b5605e41605c2722ed428cae0",
    "updated": 1782166695
  },
  "author_guid": "a23575f49d0af385314c1f02280163374297e018a692e5e4ab85eb307ebf6ebc",
  "author_name": "sami b",
  "body": "Why we rebuilt\n\nEverything starts with...",
  "published_at": 1782166695,
  "slug": "why-we-rebuilt-the-docs",
  "status": "published",
  "tags": [
    "docs",
    "meta"
  ],
  "title": "why we rebuilt the docs"
}

One posts storage, everything embedded: the author is denormalized (author_name stored next to author_guid) so list pages never join. That is the core storage principle from Storages.

Two public pages and a feed share one query

The list page

  1. Add the page

    web_page (new web page) with event blog and scope *public.

  2. Query the posts

    function_search (search) with query_bool_term_fields (fields) on status = published in query_bool_filter (filter), and query_sort_field (sorting) on published_at descending, merged by query_and (multiple), with limit 20.

  3. Render the rows

    Wire array into web_foreach (web for each) with the row template below, and set its empty pin to <p>No posts yet.</p>.

  4. Render the page

    web_render (web render) with title, description and a custom pin posts holding the html of web_foreach. Set cache seconds to let shared caches keep it.

Row template on web_foreach
<article>
  <h2><a href="/blog/{{slug}}">{{title | capitalize}}</a></h2>
  <p>{{author_name}}, {{published_at | date "DD-MM-YYYY"}}</p>
  <p>{{body | truncate 160}}</p>
</article>
Page template
<main class="blog">
  <h1>{{title}}</h1>
  <input name="q" type="search" placeholder="Search posts"
    data-action="search" data-trigger="input" data-debounce="300">
  <div id="posts">{{{posts}}}</div>
</main>

The post page

  1. Give every post a URL

    web_page with event blog/:slug and scope *public. Read slug from its params pin with object_field_getter_multiple (get fields).

  2. Fetch it

    function_search_single_result (search single) with a term on slug and a term on status = published, so drafts 404 for the public.

  3. Answer both paths

    branch (branch) on has_hits. True: web_render with a custom pin post, title from the post, and json_ld if you want structured data. False: web_render with status 404 and a short not-found template.

Page template: the post
<article>
  <h1>{{post.title | capitalize}}</h1>
  <p>{{post.author_name}}, {{post.published_at | date "DD-MM-YYYY"}}</p>
  <p>{{post.body | nl2br}}</p>
  <ul>
    {{for tag in post.tags}}
      <li>{{tag}}</li>
    {{/for}}
  </ul>
</article>

The feed

The same list query behind on_startup_register_uri_get (On API GET), answered with httpconnection_set_json (reply in JSON), gives external readers JSON. See Creating a REST API.

Publishing is a status change on an editor page

Put the editor pages in a blueprint of their own, with a custom scope such as editor. An action without scopes of its own requires what every page of its blueprint requires, so this keeps public reads and editor writes cleanly apart.

  1. List everything

    web_page with event blog-admin, the same search without the status filter, and web_foreach with the stored row template {{> post-admin-row}}. Rows are keyed by the document's guid.

  2. Write drafts

    A form with data-action="save-post" runs web_action (on web action) save-post. Build the slug once from the title with value_to_lowercase (convert to lowercase) and value_replace (replace) (/[^a-z0-9]+/g to -). Create with function_create_document_from_mutations (create document), using the slug as guid_seed and status: draft.

  3. Refuse a duplicate

    A slug that already exists makes that block answer success false with DOCUMENT_GUID_ALREADY_EXISTS. Answer with web_errors (web form errors) on the title field. On success, web_redirect (web redirect) back to /blog-admin.

  4. Publish in place

    The row's button runs web_action publish. Its key pin is the document's guid: function_update_document_mutations (update document) sets status: published and published_at, web_template (web template) renders {{> post-admin-row}} with the updated object as data and the guid as key, and web_replace (web replace) swaps that one row.

Stored template: post-admin-row
<tr>
  <td>{{title | capitalize}}</td>
  <td>{{status}}</td>
  <td>
    <a href="/blog-admin/edit/{{_meta.guid}}">Edit</a>
    {{if status == 'draft'}}
      <button type="button" data-action="publish" data-loading-text="Publishing">Publish</button>
    {{/if}}
  </td>
</tr>

Edits create revisions automatically, and the public list only ever shows published.

Search runs while the visitor types

The input on the list page runs an action named search 300 ms after typing stops; a request still running is cancelled.

  1. Read the term

    web_action search; the term is q on its form pin. Lowercase it with value_to_lowercase.

  2. Search

    Feed query_bool_simple_query_string_field (simple query string) on title and body into query_bool_must (must), merged with the same published filter and sort. Lowercase those fields at write time, per search optimization.

  3. Answer with the rows

    web_foreach with the same row template, and web_update (web update) with target #posts and its html.

What to reuse

  • One storage per content type; embed what lists display.
  • Status fields drive visibility. Never delete content, transition it.
  • Public reads and editor writes are separate pages with separate scopes.
  • The same query serves the page, the API and the search box.

Next steps

Lists and Tables Rows, keys and empty states with web_foreach. SEO and Sharing The head tags, Open Graph and structured data a post gets. Storage Examples More production-shaped data models. E-Commerce Catalog The same pattern at product scale.

Frequently asked

How do I keep draft blog posts out of the public list in RUAL?

Drive visibility with a status field. The public pages query status = published, and the post page adds a term on status so drafts return 404 for the public. Publishing is a mutation from draft to published plus a published_at timestamp: never delete content, transition it.

Why does the RUAL blog example store the author name on every post?

RUAL storage has no joins, so the author is denormalized: author_name is stored next to author_guid, and list pages render names without fetching a second document.

How do I add a search box to a RUAL blog?

Feed query_bool_simple_query_string_field on title and body into the same list query that serves the index page, and store those fields in lowercase at write time so the case-sensitive search matches.