# $kirby undefined inside snippet

**URL:** <https://forum.getkirby.com/t/kirby-undefined-inside-snippet/17637>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 18, 2020, 10:13pm UTC](https://forum.getkirby.com/t/kirby-undefined-inside-snippet/17637 "2020-03-18T22:13:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)\
**Post date:** [March 18, 2020, 10:13pm UTC](https://forum.getkirby.com/t/kirby-undefined-inside-snippet/17637/1 "2020-03-18T22:13:33Z")

</div>

Hi  
I’m trying to build my own sitemap and `$kirby` doesn’t seems to work (`Undefined variable` message). I just set this foreach loop inside a `/snippets/sitemap.php` file

```
 <?php foreach($kirby->languages() as $language): ?>
    <xhtml:link hreflang="<?= $language->code() ?>" href="<?= html($page->url($language->code())) ?>" rel="alternate" />
 <?php endforeach ?>

```

This is my entire code :

```
<?= '<?xml version="1.0" encoding="utf-8"?>'; ?>
<?= '<?xml-stylesheet type="text/xsl" href="https://gitcdn.xyz/repo/pedroborges/xml-sitemap-stylesheet/master/sitemap.xsl"?>' ; ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <?php foreach ($pages as $p): ?>
    <?php if (in_array($p->uri(), $ignore)) continue ?>
    <url>
        <loc>
            <?= html($p->url()) ?>
        </loc>
        <lastmod>
            <?= $p->modified('c', 'date') ?>
        </lastmod>
           
    <?php foreach($kirby->languages() as $language): ?>
    <xhtml:link hreflang="<?= $language->code() ?>" href="<?= html($page->url($language->code())) ?>" rel="alternate" />
    <?php endforeach ?>

        <priority>
            <?= ($p->isHomePage()) ? 1 : number_format(0.5 / $p->depth(), 1) ?>
        </priority>
        <changefreq><?php if ($p->isHomePage()): ?>daily<?php elseif($p->depth() == 1): ?>weekly<?php elseif($p->depth() > 1): ?>monthly<?php endif ?></changefreq>
        <?php foreach ($p->images() as $image) : ?>
        <image:image>
            <image:loc>
                <?= html($image->url()) ?>
            </image:loc>

            <?php if ($image->caption()->isNotEmpty()) : ?>
            <image:caption>
                <![CDATA[<?= $image->caption() ?>]]>
            </image:caption>
            <?php elseif ($image->alt()->isNotEmpty()) : ?>
            <image:caption>
                <![CDATA[<?= $image->alt() ?>]]>
            </image:caption>
            <?php endif ?>

            <image:license>
               http://creativecommons.org/licenses/by-nc-nd/4.0/
            </image:license>
        </image:image>
        <?php endforeach ?>
    </url>
    <?php endforeach ?>
</urlset>

```

My languages settings looks correct on the front page, don’t know why kirby couldn’t be define here…

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 18, 2020, 10:33pm UTC](https://forum.getkirby.com/t/kirby-undefined-inside-snippet/17637/2 "2020-03-18T22:33:37Z")

</div>

Where is this snippet called?

---

<div class="post-metadata">

**Author:** ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)\
**Post date:** [March 18, 2020, 10:43pm UTC](https://forum.getkirby.com/t/kirby-undefined-inside-snippet/17637/3 "2020-03-18T22:43:02Z")

</div>

It is called by a route inside config like this [page](https://getkirby.com/docs/cookbook/content/sitemap#create-new-routes) explain :

```
'routes' => [
  [
    'pattern' => 'sitemap.xml',
    'action' => function() {
        $pages = site()->pages()->index();

        // fetch the pages to ignore from the config settings,
        // if nothing is set, we ignore the error page
        $ignore = kirby()->option('sitemap.ignore', ['error']);

        $content = snippet('sitemap', compact('pages', 'ignore'), true);

        // return response with correct header type
        return new Kirby\Cms\Response($content, 'application/xml');
    }
  ],
  [
    'pattern' => 'sitemap',
    'action' => function() {
      return go('sitemap.xml', 301);
    }
  ]
],
```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 18, 2020, 10:47pm UTC](https://forum.getkirby.com/t/kirby-undefined-inside-snippet/17637/4 "2020-03-18T22:47:03Z")

</div>

Ah, that’s what I thought. If you call a snippet outside a template context, you have to pass the variables to it (you can’t use `$kirby` etc. outside a template context without defining them).

```auto
$content = snippet('sitemap', ['pages' => $pages, 'ignore' => $ignore, 'kirby' => kirby()], true);

```

As an. alternative, you could use `kirby()` instead of `$kirby` inside your snippet.

---

<div class="post-metadata">

**Author:** ![Oziris](https://avatars.discourse-cdn.com/v4/letter/o/df788c/32.png) [@Oziris](https://forum.getkirby.com/u/Oziris)\
**Post date:** [March 18, 2020, 10:50pm UTC](https://forum.getkirby.com/t/kirby-undefined-inside-snippet/17637/5 "2020-03-18T22:50:14Z")

</div>

Great, this code works, good to know, I would never guess.  
Thks a lot  
Good night 🙂
