Custom share template

For full control over the HTML structure of a shared page — beyond what custom CSS, JS, or HTML snippets allow — you can replace the page template entirely using the ~shareTemplate relation.

To do so:

  1. Create a Code note with the language Embedded JavaScript (EJS).
  2. For a shared note to apply the newly created template, apply the ~shareTemplate relation pointing to the note created at step (1). Make use of Attribute Inheritance to apply it to multiple shared notes.

There are two important constraints to be aware of:

  • Because EJS templates execute arbitrary server-side JavaScript, ~shareTemplate only takes effect when backend scripting is enabled. If it is disabled (see Security), the relation is ignored and the default template is used. For the same reason, only apply templates from notes you trust.
  • The template note must be part of the shared subtree (just like ~shareCss, ~shareJs) in order for it to be loaded. To prevent it from being shown in the navigation, apply #shareHiddenFromTree to it.

Content of the share template

Use the original template as reference when creating a new share template.

Available variables

Your template is rendered with a context object exposing the note and its rendering environment. The most useful values are:

VariableDescription
noteThe note being rendered. Use it to read attributes (note.getLabelValue(...)), note.title, child notes, etc.
contentThe note's already-rendered HTML content, as a string.
headerExtra HTML to place in the document head for this note (used by some note types).
isEmptytrue when the note has no content of its own.
headThe values of the page's <head>: title (the note's title, followed by the site's), description (#shareDescription), noIndex (#shareDisallowRobotIndexing) and openGraph with url, domain, image, color and card (summary_large_image with an image, summary without). A value whose label is not set is null. metaTags lists the description, OpenGraph and Twitter <meta> tags of the values that are set, each with attribute (name or property), key and content.
logoThe site logo above the navigation tree and, on a narrow screen, in the header: href (#shareRootLink, otherwise the site's root page), image (the ~shareLogo image, or null), icon (the icon classes of the site's root note, shown when there is no image), and width and height for the image.
navigationThe navigation tree of the site: its visible pages, each with title, href, isExternal, type, icon, isActive (the page being shown), isExpanded (that page or one of its ancestors) and children.
contentClassesThe classes of the content element: type-<note type>, ck-content for text and Markdown notes, full-height for a note that takes the whole page (such as a PDF, a web view, a calendar, a geo map or an image), and no-content when the note has no content of its own.
childLinksThe links to the note's visible children, each with title, href, isExternal, type, icon (its icon classes), excerpt (its #shareDescription or the start of its text, with a line break between paragraphs, or null) and children (when it has no excerpt, its first ten visible children, each with title, href, isExternal and icon). Empty for a note that is not a collection, a text note or a code note, for a note that takes the whole page, and with #hideChildrenOverview.
childLinksLayoutHow the default template lays childLinks out: list for a collection whose view type is list, grid otherwise.
prevNextThe pages before and after the note in the navigation tree, as previous and next, each with title and href, or null. Both null for a note that takes the whole page.
languageThe languages of the page: page with the lang and dir of the application's language, and content with those of the note's content language, or null when they are the same.
lastUpdatedWhen the note was last changed, as iso for a <time> element and as text in the application's language, or null, which it also is for a note that takes the whole page.
showTitleWhether the default template shows the note's title above the content. Always true.
titleIconThe icon classes of the note, which the default template shows beside the title in a row of its own for a note that takes the whole page; null for any other note.
snippetsThe HTML of the ~shareHtml snippets for each location, such as snippets["head:end"]. Every location is present, empty when no snippet goes there.
subRootThe root of the shared subtree as { note, branch } — handy for a site-wide title or logo.
cssToLoad / jsToLoadArrays of stylesheet / script URLs the default theme would inject (includes anything added via ~shareCss / ~shareJs).
faviconUrl / logoUrlResolved favicon and logo URLs. Without ~shareLogo, logoUrl is the Trilium logo.
fontPreloadsThe icon fonts to preload, each with href and type (the font's media type): Boxicons, and the icon packs used by the logo, the navigation tree or the subpages. The default template adds a <link rel="preload" as="font" crossorigin> for each, so that the icons do not appear only after the first paint.
isStatictrue during a static HTML export, false for a live server render.
tThe i18next translation function.
utilsHelper utilities such as slugify() and stripTags().

Error handling

If your template throws an error while rendering, Trilium logs the error and quietly falls back to the default template, so a broken template never takes the shared page down.

Splitting a template into partials

A template can pull in other EJS notes as partials. Create them as child notes of the template note (each also a code / application/x-ejs note) and reference them by title:

<%- include("header") %>

<main>
    <h1><%= note.title %></h1>
    <%- content %>
</main>

<%- include("footer") %>

Here header and footer are the titles of child notes of the template. Only direct children are resolvable, and they must be EJS code notes.