Global `$site`, `$page`, `$pages` in route snippet

It worked like you said it would. :slight_smile:

Class in a plugin:

<?php
class SnippetPreview extends Kirby\Component\Template {
	public function render($template, $data = [], $page = null) {
		$file = $template;
		$data = $this->data($page, $data);

		if(!file_exists($file)) {
			throw new Exception('The template could not be found');
		}

		$tplData = tpl::$data;
		tpl::$data = array_merge(tpl::$data, $data);
		$result = tpl::load($file, null);
		tpl::$data = $tplData;

		return $result;
	}
}

It’s been shorten down quite a bit to only contain what is really needed.

Inside a route, or just run it late:

$SnippetPreview = new SnippetPreview(kirby());

$path = 'some/path/to/snippet.php';
$data = array();
$page = page('about');

$data = $SnippetPreview->render($path, $data, $page);

echo $data;
  • $path to the snippet is required.
  • $data is optional data values for the template.
  • $page is needed to get values from, for example $page->title() .