Typical examples in the Starterkit are the
menu.php snippets that are used in every template.
Snippets are stored in the
/site/snippets folder and like templates, they have the extension
snippet() helper function lets you include snippets into your templates:
|Snippet||In your template|
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|
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.
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:
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:
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:
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].
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
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
Code that is printed directly between the
endsnippet() calls is captured into the default slot:
The default slot can be accessed with the
$slot variable or with
You can combine the
default slot with named slots by calling the
slot() function without a name:
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:
If you want to pass a variable in addition to slots, you can combine slots with the
You can nest snippets (with slots and/or data) inside slots of other snippets:
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:
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:
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: