Skip to content

Pages, routes and menus ​

This guide covers how URLs become pages, how blocks link to each other, and how a theme handles menus, languages and forms.

Templates and pages ​

A site has two kinds of page documents. Both are built in Studio out of your blocks.

  • A page lives at one URL. /about, /cart and /search are pages.
  • A route template is one layout for many URLs. The template at /products/:slug renders every product in the shop.

Blocks on a route template fill themselves in from the object the URL points at. The product page shows whichever product :slug names.

Your theme can ship the pages a new site starts with. Declare them in nuxt.config.ts:

ts
eldra: {
  templates: [
    {
      routePattern: '/products/:slug',
      schemaApiId: 'catalog:product',
      title: 'Product',
      blocks: [{ id: 'product-detail', apiId: 'product-detail', data: { /* … */ } }],
    },
    {
      page: { slug: 'cart' },
      title: 'Your cart',
      blocks: [
        { role: 'header' },
        { apiId: 'cart', data: { variant: 'page' }, required: true },
        { role: 'footer' },
      ],
    },
  ],
  templateRoles: {
    header: { apiId: 'navigation', data: { /* … */ } },
    footer: { apiId: 'footer', data: { /* … */ } },
  },
}

How it works:

  • An entry with routePattern and schemaApiId is a route template. schemaApiId is catalog:product, catalog:collection, catalog:category or home.
  • An entry with page: { slug } is a page at /<slug>.
  • { role: 'header' } places the site's shared header. Its content comes from templateRoles, so every seeded page shares one header and one footer.
  • required: true locks a block on a page. The merchant can edit it but not delete it. A cart page without a cart would make no sense.
  • data follows the same rules as a block's mock.json: no demo images.

The platform creates these on the site's first deploy. It skips any page or template the site already has, so a merchant's edits are never overwritten.

The starter seeds a product, collection, category and home template, plus /cart, /wishlist, /search and /products pages. It builds them from the files in pages/*.page.json.

More: Seeded templates and pages and Seeding default templates and pages.

How a URL finds its page ​

Every URL goes through one catch-all page, app/pages/[...slug].vue. It asks useEldraPage() what to render:

ts
const { page, template, catalog, layout, blocks, reusableComponentProjection, pending, error } =
  useEldraPage();

The lookup runs in this order:

  1. A page whose path is the URL wins.
  2. Otherwise, the first route template whose pattern matches. A catalog template then looks the slug up in the shop's catalog.
  3. Otherwise, nothing matches. page and template are both null, and you show your "not found" page.

If the platform could not be reached, error is set instead. Show an error, not "not found".

Product and collection patterns end in :slug, one path segment. The category pattern is /categories/:path*, because a category's URL holds its parents too: /categories/kitchen/mugs.

Your app.vue must pass the route key the module provides:

vue
<template>
  <NuxtPage :page-key="eldraRouteKey" />
</template>

Without it, a page can render twice when the host adds a trailing slash.

New pages need a build

The deployed site is static. A page the merchant creates becomes a URL on the next build, and publishing in Studio starts that build for you.

More: Route resolution on a generated site.

Linking between pages ​

Use a link field for anything the merchant points somewhere. It stores the destination itself, not a typed URL, so renaming a product never breaks the link.

json
{
  "fieldId": "button",
  "name": "Button",
  "type": "link",
  "metadata": { "kinds": ["product", "collection", "page", "url"] }
}

The kinds are product, collection, category, entry, page and url. Leave out kinds to offer them all.

Turn the stored value into an href with useEldraLink():

ts
import { useEldraLink } from '@eldrajs/theme-vue';

const resolveLink = useEldraLink();
const button = computed(() => resolveLink(data.value.button));
// button.value?.href  -> '/products/ash-glaze-mug', or null
// button.value?.label -> the merchant's label, else the target's own title

Two rules:

  • href is null when the target is gone. Show the label as plain text, or nothing. Never a dead link.
  • Same-site links go through EldraRouterLink. Pass it as the as prop of @eldrajs/ui's Link or Button. You get client-side navigation and the right language prefix.

Blocks and fields has a whole block using a link field.

More: Links.

A menu is a list of link fields with metadata.tree. This is the starter footer's link groups. Each group is one link: its label is the column heading, and its children are the column's links.

json
{
  "fieldId": "groups",
  "name": "Link groups",
  "type": "list",
  "metadata": {
    "item": {
      "fieldId": "group",
      "name": "Group",
      "type": "link",
      "localized": true,
      "metadata": { "tree": true }
    }
  }
}

tree: true gives each link one level of children. The merchant picks "None" as the kind for a heading that goes nowhere itself.

Render it with the same useEldraLink(). Each resolved link carries its children:

vue
<script setup lang="ts">
import { computed } from 'vue';
import { Link } from '@eldrajs/ui';
import { useEldraLink } from '@eldrajs/theme-vue';
import { useBlockData } from '../../app/composables/useBlockData';
import EldraRouterLink from '../../app/components/EldraRouterLink.vue';
import { isInternalHref } from '../../app/utils/links';

const props = defineProps<{ entry: EldraBlockEntry<'footer'> }>();
const { data } = useBlockData(props, 'footer');
const resolveLink = useEldraLink();

const groups = computed(() =>
  (data.value.groups ?? []).flatMap((value) => {
    const group = resolveLink(value);
    return group?.label ? [group] : [];
  })
);
const linkAs = (href: string) => (isInternalHref(href) ? EldraRouterLink : undefined);
</script>

<template>
  <nav aria-label="Footer">
    <div v-for="(group, i) in groups" :key="i">
      <h2>{{ group.label }}</h2>
      <ul>
        <li v-for="(child, j) in group.children" :key="j">
          <Link v-if="child.href" :href="child.href" :as="linkAs(child.href)">{{
            child.label
          }}</Link>
          <span v-else>{{ child.label }}</span>
        </li>
      </ul>
    </div>
  </nav>
</template>

For a mega menu, tree takes an object instead. It can add column sections and promotional cards. The starter's header (blocks/navigation) is the full example.

More: Declaring a menu.

Content languages ​

The merchant chooses the languages the shop publishes in. The default language lives at /. Every other language gets a path prefix:

/products/ash-glaze-mug         the default language
/is-IS/products/ash-glaze-mug   the same page in Icelandic

The slug stays the same in every language. The build writes each page once per language. It also adds <html lang> and hreflang links for you.

useEldraLocale() gives you the rest. Here is a minimal language switcher:

vue
<script setup lang="ts">
import { useEldraLocale } from '@eldrajs/theme-vue';

const locale = useEldraLocale();
</script>

<template>
  <select
    v-if="locale.supported.length > 1"
    aria-label="Language"
    :value="locale.active"
    @change="locale.select(($event.target as HTMLSelectElement).value)"
  >
    <option v-for="tag in locale.supported" :key="tag" :value="tag">{{ locale.name(tag) }}</option>
  </select>
</template>
  • supported lists every language, the default first. Show no switcher when there is only one.
  • name(tag) is the language's own name, like "íslenska (Ísland)".
  • select(tag) goes to the current page in that language.
  • path(href) adds the current prefix to a same-site path.

Links keep the language on their own when they go through useEldraLink() or EldraRouterLink.

Two kinds of link need the prefix by hand

A plain <form action> and an <a> you write without as skip the router. Put their path through useEldraLocale().path().

More: Content locales and locale-prefixed routing and Content locales in the starter.

Forms ​

Merchants build forms in Studio's Forms page. The platform stores each form, receives the submissions, filters spam and sends the emails. Your theme does two things.

Seed the forms a new site needs, in nuxt.config.ts:

ts
eldra: {
  formSeeds: [
    {
      key: 'contact',
      name: 'Contact',
      fields: [
        { id: 'name', type: 'text', required: true, label: { 'en-US': 'Name', 'is-IS': 'Nafn' } },
        { id: 'email', type: 'email', required: true, label: { 'en-US': 'Email', 'is-IS': 'Netfang' } },
        { id: 'message', type: 'textarea', required: true, label: { 'en-US': 'Message', 'is-IS': 'Skilaboð' } },
      ],
      actions: { notify: true, confirm: false },
      success: { message: { 'en-US': 'Thanks, we will reply soon.', 'is-IS': 'Takk, við svörum fljótlega.' } },
    },
  ],
}
  • Field types are text, email, phone, textarea, select, checkbox, consent, number and url.
  • notify emails the merchant. confirm emails the visitor a copy. subscribe adds the visitor to a newsletter list, with double opt-in.
  • The platform creates a seeded form only when its key is new, and never over the merchant's edits.

Place them with a block. The starter's form block renders any form by its formKey. The merchant adds the block and types the key, such as contact. The build bakes each seeded form into the page, and the browser then loads the live version, so a merchant's changes show up without a rebuild.

More: Forms and the starter's Forms.

Eldra