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 ), 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 :
Should work in the same way as for single snippets:
<?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.
You don’t have to pass $page/$site/$kirby to snippets (unless you have redefined those variables)
What does this render then?
That’s what I thought and hoped too!
It renders the site url and just stops after the “#”
localhost:8888/#
Could you post some more context of how you are using the snippet? In a loop maybe?
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.
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?
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.