Using Partials in Pages

Partials are reusable templates that you include from other templates. They live in _private/ folders, which are never routed.

Including a partial

<%- include('nav') %>
<%- include('card', { product }) %>
  • include(name, locals?) renders a template from a _private/ folder and returns its text. Use <%- so its HTML isn't escaped.
  • name may omit the extension; .ets, .ejs, .md, .jsx and .tsx are tried, in that order (see File types).
  • The partial sees the same ctx, plus locals as top-level variables.
  • locals is also the whole object, as in EJS. Use it for optional values: <%= title %> throws if title wasn't passed, but <% if (locals.subtitle) { %> doesn't.
  • Including more than 32 levels deep is an error.

Where partials are found

pb_pages/
  _private/
    header.ets
    footer.ets
  blog/
    _private/
      sidebar.ets
    posts/
      _private/
        post-card.ets
      index.ets

Pages looks in _private/ in the current template's folder, then in each parent folder up to pb_pages/.

<!-- In blog/posts/index.ets -->
<%- include('post-card', { post }) %>  <!-- blog/posts/_private/post-card.ets -->
<%- include('sidebar') %>              <!-- blog/_private/sidebar.ets -->
<%- include('header') %>               <!-- _private/header.ets -->

A name starting with / looks only in pb_pages/_private/:

<%- include('/header') %>  <!-- always pb_pages/_private/header.ets -->
<%- include('/blog/_private/sidebar') %>  <!-- a path that names a _private folder: blog/_private/sidebar.ets -->

The extension is optional. A dotted name such as include('nav.main') finds nav.main.ets.

This means you can:

  • Keep section-specific partials in that section's _private/.
  • Hoist a shared partial up to a common parent.
  • Override a parent's partial with a local one of the same name, and still reach the root one with /name.

Passing data

Pass only what the partial needs:

<%- include('user-card', { name: user.getString('name'), joined: user.getString('created') }) %>
<!-- _private/user-card.ets -->
<div class="card">
  <h3><%= name %></h3>
  <p>Joined <%= joined %></p>
</div>

Partials can also read ctx, data and params directly. Passing values explicitly keeps them easy to reuse.

Markdown partials

A .md partial is rendered like a Markdown page: its tags run as plain JavaScript, then Markdown (see Markdown):

<%- include('intro') %>  <!-- _private/intro.md -->

JavaScript modules

_private/ folders also hold TypeScript modules. Load them with ctx.resolve(), which uses the same lookup.