Skip to content

Kirby 4.5.0

snippet()

Embeds a snippet from the snippet folder

snippet($name, $data = [ ], bool $return = false, bool $slots = false): Kirby\Template\Snippet|string|null

Parameters

Name Type Default
$name * mixed
$data mixed [ ]
$return bool false
$slots bool false

Return type

Kirby\Template\Snippet|string|null

Example

// Include the header snippet
<?php snippet('header') ?>

// Set the $class variable inside the snippet to "blog"
<?php snippet('header', ['class' => 'blog']) ?>

// Return the rendered snippet code
<?php $header = snippet('header', [], true); ?>
<?php $header = snippet('header', ['class' => 'blog'], true); ?>

Snippet alternatives

You can define an array of snippet alternatives if the first snippet cannot be found:

<?php snippet(['snippet1', 'snippet2', 'snippet3']) ?>

This is useful if you want to provide fallbacks for a snippet based on page input, in case the snippet does not exist:

<?php snippet(['articles/' . $page->postType(), 'articles/default']) ?>