Reference

Guide

snippet()

Embeds a snippet from the snippet folder

snippet(string|array $name, array|object $data = [ ], bool $return = false): string

Parameters

Name Type Default Description
$name string|array
$data array|object [ ]
$return bool false

Return type

string

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

Since 3.2.0

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']) ?>

Source code

kirby/config/helpers.php#L677