Snippets
Snippets are smaller blocks of code that you can reuse across templates. They are ideal for keeping your templates clean and maintainable.
Use cases
Typical examples in the Starterkit are the header.php
, footer.php
and menu.php
snippets that are used in every template.
Snippets are stored in the /site/snippets
folder and like templates, they have the extension .php
.
Make your life easier with snippets
A great way to clean up your templates and reuse parts of your templates multiple times.
Using snippets
Kirby's snippet()
helper function lets you include snippets into your templates:
Snippet | In your template |
---|---|
/site/snippets/header.php |
<?php snippet('header') ?> |
/site/snippets/menu.php |
<?php snippet('menu') ?> |
/site/snippets/footer.php |
<?php snippet('footer') ?> |
If you want to organize snippets in subfolders, you can do so. In that case, you have to pass the path to the snippet to the snippet function:
Snippet | In your template |
---|---|
/site/snippets/basics/header.php |
<?php snippet('basics/header') ?> |
Let's clean up a template
The most basic use case for snippets is splitting the header and footer into separate snippets. Let's explore how we can break up the basic template from the last section and make parts of it reusable:
The code above looks fine as long as we just have a single template. But with every template we add, we would have to repeat the same stuff over and over again. What if we wanted to make a change to the header or footer? We would have to touch every single file.
Well, we don't want to waste our precious time, so let's clean this up a bit!
Now that we have separated those parts, the final template looks very neat:
No matter how many more templates we will add to our site, the header and footer part are now only two lines of code. All we need to modify is the main part of the template.
Passing variables to snippets
Sometimes it is useful to pass a variable to a snippet.
With the above code, /site/snippets/mysnippet.php
will receive a title variable with the content "Hello!" that we can now echo in our snippet:
Since 3.9.0
Snippets always receive the following variables from Kirby:
Variable | Type | Value |
---|---|---|
$slot |
Kirby\Template\Slot |null |
Contents of the default slot if it exists |
$slots |
Kirby\Template\Slots |
Collection of all slots |
You can read more about the slots feature below.
Real life example: list of blog articles
The template
In this example, we pass the $article
variable (which is a page object) to the snippet and can then fetch the data from that page in our snippet:
The article.php
snippet
Pro tip
Because the variable name inside the template and snippet is the same, we can use PHP's compact()
function to make the code shorter. This is especially useful if you want to pass multiple variables with their current name:
The compact()
function takes the variable values from the current scope and compacts them into an array. So it's the same as writing ['article' => $article]
.
Since 3.9.0
Passing slots to snippets
Snippets with slots
Check out our extensive screencast to learn more about snippets with slots.
Data variables are useful if you want to pass whole objects (like the page object in the example above) or other values that you have already stored in variables.
Sometimes, passing data isn't enough, for example when you want to pass full code blocks into snippets and output them inside the snippet. For this you can use snippets with slots:
The contents of each slot are captured and passed to the snippet in the $slots
variable:
The names header
and body
are just examples. You can use as many slots as you like and give them names that fit your use case. Every slot that isn't passed from the template will be set to null
.
Default slot
Code that is printed directly between the snippet()
and endsnippet()
calls is captured into the default slot:
The default slot can be accessed with the $slot
variable or with $slots->default()
:
You can combine the default
slot with named slots by calling the slot()
function without a name:
Layout snippets
If a snippet is still open at the end of the template, Kirby automatically closes it. This can be used for layout snippets, i.e. snippets that contain a base layout that is shared between templates:
Combining slots with data
If you want to pass a variable in addition to slots, you can combine slots with the $data
parameter:
Nesting snippets
You can nest snippets (with slots and/or data) inside slots of other snippets:
Snippet alternatives
You can define an array of snippet alternatives if the first snippet cannot be found:
This is useful if you want to provide fallbacks for a snippet based on page input, in case the snippet does not exist:
Assigning a snippet to a variable
The snippet()
function has a third, optional parameter of type boolean
. If set to true
, Kirby returns the parsed content instead of directly echoing it. With this option, snippets can be used in a variety of situations, not just inside page templates:
Since 3.9.0
If you use the slots
parameter, the snippet()
helper always returns the snippet object. In this case you can use the $snippet->render()
method to pass the slots to the snippet, render the snippets contents and store them in a variable: