# Passing the data of the $page to a snippet

**URL:** <https://forum.getkirby.com/t/passing-the-data-of-the-page-to-a-snippet/21725>\
**Category:** Questions\
**Tags:** v3\
**Created:** [March 23, 2021, 10:16pm UTC](https://forum.getkirby.com/t/passing-the-data-of-the-page-to-a-snippet/21725 "2021-03-23T22:16:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![exeldoc](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/exeldoc/32/6224_2.png) [@exeldoc](https://forum.getkirby.com/u/exeldoc)\
**Post date:** [March 23, 2021, 10:16pm UTC](https://forum.getkirby.com/t/passing-the-data-of-the-page-to-a-snippet/21725/1 "2021-03-23T22:16:43Z")

</div>

Hello there,  
I try to figure out how I can use data in snippets from a page, that I want to clean up with those snippets.  
According to the Snippet page ([Snippets | Kirby CMS](https://getkirby.com/docs/guide/templates/snippets)), it should work like this:

```
<?php snippet('mysnippet', ['title' => 'Hello!']) ?>

```

But i what to use e.g. `$page->anyFieldOfThePage()` in a snippet.

So I found this one: [Passing data to alternative snippets](https://forum.getkirby.com/t/passing-data-to-alternative-snippets/17550/5):

> [@Passing data to alternative snippets](https://forum.getkirby.com/t/passing-data-to-alternative-snippets/17550/5):
>
> Should work in the same way as for single snippets:
> 
> ```auto
> <?php snippet(['snippet1', 'snippet2', 'snippet3'], ['page' => $page]) ?>
> 
> ```

Unfortunately it is not working in my case:  
this is in a template:

```
<?php snippet('controllers_w',['page' => $page]) ?>

```

This is in the snippet controllers\_w:

```
<a href="<?= $site->url() ?>/#<?= $page->artist_Id() ?>">
  
</a>

```

_Basically i want to link to a section with a specific Id on the index. Maybe it could also be done nicer, but actually it is more an example of the problem_

It is not getting the `artist_Id` from the page…  
I guess using $page in a snippet is a very common thing to do, but I didn’t really understand it yet.

---

<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 23, 2021, 11:06pm UTC](https://forum.getkirby.com/t/passing-the-data-of-the-page-to-a-snippet/21725/2 "2021-03-23T23:06:03Z")

</div>

You don’t have to pass $page/$site/$kirby to snippets (unless you have redefined those variables)

> [@exeldoc](#):
>
> ```auto
> <a href="<?= $site->url() ?>/#<?= $page->artist_Id() ?>">
>   
> </a>
> 
> ```

What does this render then?

---

<div class="post-metadata">

**Author:** ![exeldoc](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/exeldoc/32/6224_2.png) [@exeldoc](https://forum.getkirby.com/u/exeldoc)\
**Post date:** [March 24, 2021, 5:59am UTC](https://forum.getkirby.com/t/passing-the-data-of-the-page-to-a-snippet/21725/3 "2021-03-24T05:59:49Z")

</div>

That’s what I thought and hoped too!  
It renders the site url and just stops after the “#”  
`localhost:8888/#`

---

<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 24, 2021, 8:04am UTC](https://forum.getkirby.com/t/passing-the-data-of-the-page-to-a-snippet/21725/4 "2021-03-24T08:04:41Z")

</div>

Could you post some more context of how you are using the snippet? In a loop maybe?

---

<div class="post-metadata">

**Author:** ![exeldoc](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/exeldoc/32/6224_2.png) [@exeldoc](https://forum.getkirby.com/u/exeldoc)\
**Post date:** [March 24, 2021, 8:44am UTC](https://forum.getkirby.com/t/passing-the-data-of-the-page-to-a-snippet/21725/5 "2021-03-24T08:44:40Z")

</div>

Template:

```
<?php snippet('header') ?>
<?php snippet('logo_full_w') ?>
  <div class="full_grid">
 <div class="youtube_video youtube_video_artwork">
  <?= youtube($page->video_url()); ?>
</div>
<div class="artist <?= $page->color() ?>">
<h2 class="space_buttom_10p center"><?= $page->name()->ktr() ?></h2>
<div class="segment_button_row space_buttom_20p">
  <?php if($page->contact()->isNotEmpty()): ?>
    <button class="<?= $page->color() ?>" type="button" name="button">
      <?= $page->contact()->ktr() ?>
    </button>
  <?php endif ?>
  <?php if($page->website()->isNotEmpty()): ?>
    <button class="<?= $page->color() ?>" type="button" name="button">
      <?= $page->website()->ktr() ?>
    </button>
   <?php endif ?>
   </div>
   </div>

 <?php snippet('controllers_w',['page' => $page]) ?>

  </div>
<?php snippet('footer') ?>

```

Here the snippet:

```
<div class="controler play space_buttom_20p">
  <a href="<?= $site->url() ?>/#<?= $page->artist_Id() ?>">
    <div class="close_svg_w ">

    </div>
  </a>
</div>
<div class="controler controler_skip space_buttom_20p">
  <a class="backward"href="#">
  <div class="back_svg_w ">

  </div>
</a>
<a class="forward" href="#">
  <div class="forward_svg_w">

 </div>
 </a>
</div>

```

**So no loop here.**  
By the way, there is a loop somewhere else on the site – there I defined a variable and passed it to a snippet (like it is meant to be done). That worked, but the page data, I can’t reach as I described.

---

<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 24, 2021, 8:57am UTC](https://forum.getkirby.com/t/passing-the-data-of-the-page-to-a-snippet/21725/6 "2021-03-24T08:57:16Z")

</div>

Sure you field name is correct and there is data in the field? So if you put the code into the template instead of the snippet, the id is rendered?

---

<div class="post-metadata">

**Author:** ![exeldoc](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/exeldoc/32/6224_2.png) [@exeldoc](https://forum.getkirby.com/u/exeldoc)\
**Post date:** [March 24, 2021, 9:36am UTC](https://forum.getkirby.com/t/passing-the-data-of-the-page-to-a-snippet/21725/7 "2021-03-24T09:36:19Z")

</div>

I am so sorry.  
I didn’t see the tree in the forest. The Structure is a bit confusing, that’s why I wanted to clean it up in the first place. 😬

So i was in the right template but not in the right blueprint/text file. It works perfectly now.  
My bad. Again I’m sorry.
