bmidget/kohana-assets

Handy library for serving up pre-compiled js and css/less files

Installs: 615

Dependents: 0

Stars: 6

Watches: 3

Forks: 4

Open Issues: 1

Language: PHP

1.5 2014-03-19 07:21 UTC

README

Create an assets object

$assets = Assets::factory();

Define hashgroups in config files. Here are some examples:

// config/js.php
$base = [
    'bs' => DOCROOT.'assets/bootstrap/dist/js/bootstrap.min.js',
    'site' => DOCROOT.'assets/js/site.js',
];

return [
    'default' => $base,
    'admin' => $base + [
        'footable' => DOCROOT.'assets/footable/js/footable.js',
        'footable-paginate' => DOCROOT.'assets/footable/js/footable.paginate.js',
        'footable-filter' => DOCROOT.'assets/footable/js/footable.filter.js',
        'footable-sort' => DOCROOT.'assets/footable/js/footable.sort.js',
        'admin' => DOCROOT.'assets/js/admin.js',
    ]
];

// config/css.php
return [
    'default' = [
        'site' => DOCROOT.'assets/less/site.less',
    ],
    'public' => [
        'site' => DOCROOT.'assets/less/site.less',
        'public' => DOCROOT.'assets/less/public.less',
    ],
    'admin' => [
        'site' => DOCROOT.'assets/less/site.less',
        'footable' => DOCROOT.'assets/less/footable.less',
        'admin' => DOCROOT.'assets/less/admin.less',
    ],
    'special_page' => [
        'site' => DOCROOT.'assets/less/site.less',
        'special' => DOCROOT.'assets/less/special.less',
    ],
];

Pre-compile assets

You can create a minion task to do this as a deployment script for your production environment.

class Task_Asset_Compile extends Minion_Task {

    protected function _execute( array $params)
    {
        $asset = Assets::factory();
        $asset->update_config();
    }

}

And in your dev environment, it's nice to update the compiled assets every refresh. Here's an example of setting this up in a Template controller:

$this->template->bind('assets', $this->_assets);

$this->_assets = Assets::factory();

if (Kohana::$environment === Kohana::DEVELOPMENT)
{
    $this->_assets->update_config();
}

And in your view template, return the script tags pointing to the proper compiled js and css files

<?=$assets->get_cached('css', true)?>
<?=$assets->get_cached('js', true)?>