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

**URL:** <https://forum.getkirby.com/t/global-site-page-pages-in-route-snippet/5407>\
**Category:** Questions\
**Tags:** v2\
**Created:** [October 5, 2016, 6:49am UTC](https://forum.getkirby.com/t/global-site-page-pages-in-route-snippet/5407 "2016-10-05T06:49:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [October 5, 2016, 6:49am UTC](https://forum.getkirby.com/t/global-site-page-pages-in-route-snippet/5407/1 "2016-10-05T06:49:07Z")

</div>

If you are not using routes, it’s in a template possible to call a snippet and inside that snippet write:

```php
echo $page->title();

```

It magically works because `$page` is like a global object that I can use in any template or snippet.

## Route

Here I call a snippet with a route. To make it aware of `$site`, `$page` and `$pages` I send it as arguments.

```php
kirby()->routes(array( 
  array(
    'pattern' => 'my/pattern/(:any)',
    'action' => function ($uid) {
      snippet('a-snippet', array(
        'site' => site(),
        'page' => page('some/page'),
        'pages' => page('some/page')->children()
      ));
      return site()->visit('about', 'de');
    }
  )
));

```

The problem is that when I call a snippet from that snippet it does not remember these objects anymore.

Is there a good way to make them global? I tried a few things but could not make it work.

## Snippets

Called from the route above.

`a-snippet`

```html
<?php echo $page->title(); // Works ?>
<?php snippet('another-snippet'); ?>

```

`another-snippet`

```html
<?php echo $page->title(); // Doesn't work ?>

```

How can I make `$site`, `$page` and `$pages` as global objects when a snippet is used in a route?

---

<div class="post-metadata">

**Author:** ![fvsch](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/fvsch/32/15908_2.png) [@fvsch](https://forum.getkirby.com/u/fvsch)\
**Post date:** [October 5, 2016, 8:30am UTC](https://forum.getkirby.com/t/global-site-page-pages-in-route-snippet/5407/2 "2016-10-05T08:30:29Z")

</div>

They’re not global per se, but added by the Template component when rendering a page:

```php
    // merge and register the template data globally
    $tplData = tpl::$data;
    tpl::$data = array_merge(tpl::$data, $data);
    // load the template
    $result = tpl::load($file, null, $return);
    // reset the template data
    tpl::$data = $tplData;

```

(In this example `$data` contains `'site'`, `'page'`, `'kirby'` and `'pages'` keys with the corresponding objects.)

On the other hand, the `snippet` helper function will simply load existing data from `tpl::$data`.

This means that if you have a route that renders a snippet, and you’re not rendering a page (or a rendering a snippet before rendering a page and calling the Template component), `tpl::$data` will probably be empty. If that doesn’t work for you, you probably need to write your own rendering function that does the same thing as `Kirby\Component\Template::render`. Or see if you can retrieve an instance of `Kirby\Component\Template` (through the registry?) and use the `render` method to render a snippet (by giving the full path to the snippet PHP file as first argument).

By the way, it seems you’re feeding your snippet a `$pages` collection with a specific pages collection. I would avoid doing that, especially if you’re going to overwrite `tpl::$data['pages']` with that specific value. Maybe use a different name? Or, if the logic in your snippet is to always use the children of the current page, maybe hardcode that in the snippet?

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [October 5, 2016, 11:50am UTC](https://forum.getkirby.com/t/global-site-page-pages-in-route-snippet/5407/3 "2016-10-05T11:50:52Z")

</div>

I see. 🙂

At the moment I will not extend the template component just for this. I have other solutions that will work ok. Good to know anyway. Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [January 13, 2017, 8:09am UTC](https://forum.getkirby.com/t/global-site-page-pages-in-route-snippet/5407/4 "2017-01-13T08:09:03Z")

</div>

This issue seems to be coming back to me from time to time. This time I need to use it to preview a snippet. For that I will need a route and somehow send arguments to the snippets. Because the snippet can use many other snippets which will probably use the `$page` object I will need to solve this issue.

Anyway I think your previous reply gave a few hints of how to solve it. Time will tell of how things goes.

---

<div class="post-metadata">

**Author:** ![jenstornell](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jenstornell/32/546_2.png) [@jenstornell](https://forum.getkirby.com/u/jenstornell)\
**Post date:** [January 17, 2017, 1:31pm UTC](https://forum.getkirby.com/t/global-site-page-pages-in-route-snippet/5407/5 "2017-01-17T13:31:17Z")

</div>

It worked like you said it would. 🙂

**Class in a plugin:**

```php
<?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:**

```php
$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()` .

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:53pm UTC](https://forum.getkirby.com/t/global-site-page-pages-in-route-snippet/5407/6 "2025-02-06T16:53:00Z")

</div>

Archived on February 6, 2025, 4:53pm UTC


