If you code a plugin and use the register set feature, you might end up with something like this:
// Blueprints
$kirby->set('blueprint', 'default', __DIR__ . DS . 'blueprints' . DS . 'default.yml');
$kirby->set('blueprint', 'about', __DIR__ . DS . 'blueprints' . DS . 'about.yml');
$kirby->set('blueprint', 'contact', __DIR__ . DS . 'blueprints' . DS . 'contact.yml');
// Snippets
$kirby->set('snippet', 'header', __DIR__ . DS . 'snippets' . DS . 'header.php');
$kirby->set('snippet', 'footer', __DIR__ . DS . 'snippets' . DS . 'footer.php');
// Templates
$kirby->set('template', 'default', __DIR__ . DS . 'templates' . DS . 'default.php');
$kirby->set('template', 'about', __DIR__ . DS . 'templates' . DS . 'about.php');
// Controllers
$kirby->set('controller', 'about', __DIR__ . DS . 'controllers' . DS . 'about.yml');
When using many things it can take time to add all the things.
Solution
For it to work out of the box you need a folder structure that looks like this:
site/plugins/my-plugin/blueprints/
site/plugins/my-plugin/snippets/
site/plugins/my-plugin/templates/
site/plugins/my-plugin/controllers/
Put this in my-plugin.php
:
$sets = array('blueprint', 'snippet', 'controller', 'template');
foreach( $sets as $set ) {
foreach( glob( __DIR__ . DS . $set . 's' . DS . '*.*') as $file ) {
$kirby->set( $set , pathinfo( $file, PATHINFO_FILENAME ), $file );
}
}
By some looping all you need is a few lines of code. The more files you need to register the more time you save.
Pitfalls
- You need to have the same filename as the name of the item you register.
- It’s quite aggressive and try to register everyting in the folders. There is no exclude feature.
Another snippet solution
I use snippets like this (similar to the Patterns plugin):
site/plugins/my_plugin/snippets/menu/menu.php
site/plugins/my_plugin/snippets/hero/hero.php
To make that work I use this loop:
foreach( glob( __DIR__ . DS . 'snippets' . DS . '*', GLOB_ONLYDIR) as $file ) {
$kirby->set( 'snippet', basename($file) , $file . DS . basename($file) . '.php' );
}
Be aware that to combine them, remove snippet
from the array, like this:
$sets = array('blueprint', 'controller', 'template');
foreach( $sets as $set ) {
foreach( glob( __DIR__ . DS . $set . 's' . DS . '*.*') as $file ) {
$kirby->set( $set , pathinfo( $file, PATHINFO_FILENAME ), $file );
}
}