Preview Mode
The preview renders your site's own templates, CSS, and JavaScript. Detect preview mode to hide elements or switch off features there – the cookie banner, for example, or page transitions. Links inside the preview follow their own rules, listed under Link Navigation.
Backend
In the preview, $page->previewMode() is true:
if ($page->previewMode()->isTrue()) {
// Page is in preview mode
}
Outside it, the content field is empty, so isFalse() holds. Wrap what the preview should not show, such as the cookie banner, in a check:
<?php if ($page->previewMode()->isFalse()): ?>
<?php snippet('cookie-banner') ?>
<?php endif ?>
The unsaved changes are on the edited model as Kirby hands it to the template – $page, or $site in a site blueprint. The same page fetched another way, such as page('about'), returns its saved content.
Frontend
In the preview, the <html> element carries a data-preview-mode attribute:
/* Hide the cookie banner in preview mode */
[data-preview-mode] .cookie-banner {
display: none;
}
/* Disable animations in preview mode */
const isPreviewMode = document.documentElement.dataset.previewMode === "true";
if (!isPreviewMode) {
// Run your animations or other features
}
Link Navigation
Clicking a link inside the preview opens the matching Panel view instead of following the link. Five kinds of links take another route:
- Anchors with a fragment stay in the preview. A fragment-only link like
#teamscrolls to its target. Any other fragment link to a page of the site, like/about#team, loads that page as saved, without unsaved changes. - Anchors marked with
data-preview-ignorebehave like ordinary links inside the preview – the preview then shows the linked page as saved. - Links outside the site – to another origin, or outside the folder of a site in a subfolder – open in a new browser tab,
mailto:andtel:links included. - Links below
/assets/or/media/do nothing – neither the Panel nor the preview moves. javascript:anddata:links do nothing either.
<a href="<?= $page->url() ?>" data-preview-ignore>Stay in the preview</a>