Skip to content
edmundask edited this page Dec 28, 2011 · 4 revisions

Sprinkle allows you pre-define assets and use them anywhere within the application. Why would you want to pre-define assets instead of just outputting them in the view files manually? Well, consider this situation.

Imagine you use jQuery v1.7.1 in your project. You load the library in each controller/view manually by specifically pointing to jquery-1.7.1.js file. After some time jQuery v1.8.2 arrives and you realize you need to use it in your project. You are then forced to update EVERY controller or view where you included jQuery 1.7.1 and replace it with a newer version. This is where pre-defined assets come into play.

Once you define a generic name for an asset, you can use it anywhere without worrying of having problems replacing it afterwards.

jquery:
  type: js
  minify: false
  combine: false
  src: http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js

You load the asset in your controllers like this:

$this->sprinkle->load('jquery');

So no matter which version of jQuery you have, you can easily replace it by changing the source file:

jquery:
  type: js
  minify: false
  combine: false
  src: http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js

That way all of your code stays intact.

Asset Versions

However, Sprinkle even takes a step further and lets you define versions of an asset.

jquery:
  type: js
  minify: false
  combine: false
  src: http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js
  versions:
    1.5.1: http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js
    1.8.2: http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js

This is equivalent to:

jquery:
  type: js
  minify: false
  combine: false
  versions:
    default: http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js
    1.5.1: http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js
    1.8.2: http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js

This is because Sprinkle allows you not to define versions by default. By setting the src parameter you automatically assign it to be the default version of the asset.

So then you can load different versions of an asset to your desire.

// Loads the default version
$this->sprinkle->load('jquery');
// I can has jQuery v1.8.2?!
$this->sprinkle->load('jquery', '1.8.2');

Swap Asset Versions

But wait, there's more! You can even swap between asset versions.

$this->sprinkle->load('jquery');
// Assume jQuery has already been loaded and you want to replace it with a different version of it
$this->sprinkle->replace('jquery', '1.8.2');

Assigning & Excluding Filters

To assign filters to an asset, simply add filters key:

application:
  type: css
  minify: true
  combine: false
  src: application.less
  filters: [lessphp, prefixr]

If you have global filters assigned for CSS/JS assets, you can exclude unnecessary filters for a specific asset. For example, in the configuration you specified:

$config['sprinkle']['autoload_css_filters'] = array('prefixr');

Then in your asset definitions file add exclude_filters:

application:
  type: css
  minify: true
  combine: false
  src: application.less
  filters: [lessphp]
  exclude_filters: [prefixr]

Clone this wiki locally