Skip to content

Instantly share code, notes, and snippets.

@adamkiss
Last active January 2, 2023 15:19
Show Gist options
  • Save adamkiss/9a616f31fcc06dbb910797e9ea6150d6 to your computer and use it in GitHub Desktop.
Save adamkiss/9a616f31fcc06dbb910797e9ea6150d6 to your computer and use it in GitHub Desktop.
Kirby: easy Simple Snippets plugin
<?php
use Kirby\Cms\App;
App::plugin('adamkiss/simple-snippets', []);
if (! function_exists('s')) {
/**
* Short, auto-return snippet call with support for auto-merging certain parameters
*
* @param string $snippetName
* @param mixed ...$data
*/
function s($snippetName, ...$data): string {
return snippet($snippetName, data: $data, return: true, slots: false);
}
}
if (! function_exists('_s')) {
/**
* Short, auto-return slot-opening snippet call with support for auto-merging certain parameters
*
* @param string $snippetName
* @param mixed ...$data
*/
function _s($snippetName, ...$data): Kirby\Template\Snippet {
return snippet($snippetName, data: $data, return: true, slots: true);
}
}
if (! function_exists('es')) {
/**
* Shortcut for "ENDSNIPPET"
*/
function es() {
return endsnippet();
}
}
if (! function_exists('snippetAttributes')) {
/**
* Take 'get_defined_vars' and return whitelisted array
*
* @param [type] $variables
* @param [type] ...$filter
* @return void
*/
function snippetAttributes($variables, ...$filter) {
return array_filter($variables, fn($k) => in_array($k, $filter), ARRAY_FILTER_USE_KEY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment