Bonsai (盆栽, lit. plantings in tray, from bon, a tray or low-sided pot and sai, a planting or plantings, is a Japanese art form using miniature trees grown in containers.
Bonsai gives you the ability to register assets to be loaded at runtime within your Laravel projects.
Simply install the package through Composer. From here the package will automatically register its service provider and Bonsai
facade.
composer require caffeinated/bonsai
First, plant your bonsai. You may optionally register assets during this time as well.
Bonsai::plant(function($asset) {
$asset->add('assets/css/bootstrap.css', 'bootstrap');
$asset->add('assets/css/test.css')->dependsOn('bootstrap');
$asset->add('assets/css/bootstrap.css', 'bootstrap'); // Duplicate assets will be caught and ignored.
$asset->add('assets/js/jquery.js', 'jquery');
$asset->add('assets/js/bootstrap.js', 'bootstrap')->dependsOn('jquery');
});
Now, to add assets at anytime (and anywhere in your code), simply call Bonsai:add()
:
Bonsai::add('assets/css/example.css');
Assets may depend on other assets being loaded before them. You can easily tell Bonsai about any dependencies your asset files may have against each other by using the dependsOn()
method.
Bonsai::add('assets/css/example.css')->dependsOn('bootstrap');
Bonsai::add('assets/css/bootstrap.css', 'bootstrap');
The above will generate the following CSS:
<link rel="stylesheet" href="assets/css/bootstrap.css">
<link rel="stylesheet" href="assets/css/example.css">
To echo out your assets within your layout, simply use the @bonsai
Blade directive, specifying the asset type you wish to output:
@bonsai('css')
@bonsai('js')