Assets
By default, all files in the assets folder of your plugin are made publicly available. With the assets extension, you can instead define which files should be published and where they are located in your plugin folder.
The extension accepts an array of key/value pairs, where the key is the path of the asset in the URL and the value the absolute path to the file:
Kirby::plugin('your/plugin', [
'assets' => [
'styles.css' => __DIR__ . '/dist/styles.css',
'scripts.js' => __DIR__ . '/dist/scripts.js',
'images/logo.svg' => __DIR__ . '/images/logo.svg'
]
]);
If you leave out the key, the path relative to the plugin root is used:
Kirby::plugin('your/plugin', [
'assets' => [
// available as `dist/styles.css`
__DIR__ . '/dist/styles.css'
]
]);
You can also pass a closure that returns the array. This is useful if you want to collect the files dynamically, as the closure is only called when the assets are needed:
use Kirby\Filesystem\Dir;
Kirby::plugin('your/plugin', [
'assets' => function () {
$assets = [];
$root = __DIR__ . '/dist';
foreach (Dir::files($root) as $file) {
$assets[$file] = $root . '/' . $file;
}
return $assets;
}
]);
The assets can then be accessed via $kirby->plugin('your/plugin')->asset('styles.css') or passed to the css() and js() helpers. Learn more about using plugin assets ›