Guides

Rendering pages

Read documents in your routes and render their bodies with PageView.

Mortar stays out of routing. Your routes decide which document to show, read it, and render it inside your own layout. Mortar renders the body.

Provide the content

Wrap your app in ContentContext with a ContentSource: two functions that list and get documents, usually your server functions.

import { ContentContext } from '@mortar-kit/core';

<ContentContext value={{ list: listDocs, get: getDoc }}>
  <Router>...</Router>
</ContentContext>

Read a document

useContent() gives a typed reader: pass a collection object and you get documents with typed fields.

import { PageView, useContent } from '@mortar-kit/core';
import { useParams } from '@solidjs/router';
import { createMemo, Show } from 'solid-js';

export default function PostRoute() {
  const params = useParams<{ slug: string }>();
  const content = useContent();
  const post = createMemo(() => content.get(posts, { slug: params.slug }));
  return (
    <Show when={post()} fallback={<NotFound />}>
      {(p) => (
        <article>
          <h1>{p().fields.title}</h1>
          <PageView bricks={bricks} entries={p().entries ?? []} />
        </article>
      )}
    </Show>
  );
}

Reads return promises. A memo holding a promise is pending until it lands, so put a <Loading> boundary around your routes. On the server the page waits for it, so the HTML holds the content.

Lists work the same way:

const latest = createMemo(() => content.list(posts, { limit: 5 }));
const inCategory = createMemo(() => content.list(products, { where: { category: categoryId() } }));

where keeps documents whose id, slug or top-level field equals each value. sort overrides the collection's order, and limit takes 1 to 100. Lists leave out bodies.

Layouts

Detail pages are your code: a post's title and date, a product's price. Keep them in layout components, so the admin's preview can render documents inside the same layouts (see The admin).

Missing documents

get returns null when there's no document. Render your own not-found view, and set the status with httpStatus(404) from @solidjs/web when rendering on the server.

PageViewfunction

import { PageView } from '@mortar-kit/core'

function PageView(props: PageViewProps): Element

Renders every entry with its brick's component. Bricks that load data read ContentContext.

Parameters

Returns

Element

useContentfunction

import { useContent } from '@mortar-kit/core'

function useContent(): ContentReader

The content reader for the current component, from ContentContext. Call it in a component body; read with it in a memo (under <Loading>). Throws if no source is provided.

Returns

ContentReader