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

Although the configuration file is well documented, here we'll overview all settings in more detail.

Asset Locations

Sprinkle allows you to define multiple locations in which it should look for assets. For the time being you can only add locations within the server where the application is running. In the future there's a possibility of adding URL support as well (for example, set up CDN URL).

$config['sprinkle']['asset_locations'] = array
(
	'assets/',
	'assets/js/',
	'assets/css/'
);

Sprinkle expects paths relative to your application base (where index.php file is located) directory. Also, it does not recursively go through folders (at least for now).

It is worth mentioning that if there are assets of the same type and file name in multiple directories, Sprinkle will only use the one that exists in the first location.

YAML Files

Using YAML files is not mandatory but very useful. It gives you a convenient way of defining assets and asset routes.

$config['sprinkle']['use_yaml'] = TRUE;

If you don't want to use YAML files, you are welcome to use good ol' PHP. Below is a comparison of asset definition in both YAML and PHP.

assets.yml

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

assets.php

$config['assets']['jquery'] = array
(
	'type'    			=>	'js',
	'minify'  			=>	false,
	'combine' 			=>	false,
	'versions'			=>	array
	(
		'default'		=>	'http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js',
		'1.5.1'  		=>	'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js',
		'1.7.1'  		=>	'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js'
	)
);

As you can see, defining assets in pure PHP can get a bit messy. However, it's up to you to choose the approach.

Minifying CSS & JS

You can set minify to TRUE for each asset. However, if you don't want to minify assets anymore, you can override this behaviour by changing minify_css and minify_js values.

$config['sprinkle']['minify_css'] = FALSE;
$config['sprinkle']['minify_js'] = FALSE;

Sprinkle uses Filters for asset processing and is flexible enough to allow you to choose a specific filter for CSS and JS minification.

$config['sprinkle']['minify_css_filter'] = 'cssmin';
$config['sprinkle']['minify_js_filter'] = 'jsmin';

This is useful if you decide to implement your own filter for minifying assets. There's a dedicated Filters wiki page for more information.

Combining Assets

Like with minification, you can override this behavior globally.

$config['sprinkle']['combine'] = FALSE;

Auto-loading Asset Filters

When you are working with specific filters that need to be applied to every CSS/JS asset, assigning the filters to each asset may become somewhat inconvenient. However, Sprinkle allows you to specify those global filters in the configuration.

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

For example, you want to apply prefixr filter to every CSS asset:

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

On the other hand, if you assign the filters globally, you may need to exclude them for some assets. To learn more, read the Defining Assets wiki page.

Cache Directory

When you apply filters to assets or combine them, they need to be saved somewhere. Sprinkle allows you to set the location.

$config['sprinkle']['cache_dir'] =  'assets/cache/';

Cache Expiration

To avoid processing assets on each request, Sprinkle caches them. By default cached files stay there indefinitely (the value is set to 0). However, if you want the cached files to live only for a limited amount of time, you can set the time in seconds.

// Cached files only have one hour to live!
$config['sprinkle']['cache_expiration'] = 3600;

When the cache becomes stale, assets are then re-cached.

cURL

By default Sprinkle doesn't use cURL for fetching assets from remote locations as not every server configuration has cURL enabled. Instead file_get_contents() is used. However, if you for some reason want to use cURL, you have a choice!

$config['sprinkle']['use_curl'] = TRUE;

Note that for getting the time of when the file was last modified Sprinkle still uses file_get_contents(). It seems cURL is very slow when sending HEAD requests.

Asset Processing

If you have set cache expiration, expired assets will be processed, cached and processed again when the cached file expires. Fetching assets from remote servers and applying filters can cost you valuable execution time.

Even if you let the cache last for infinite amount of time, Sprinkle will always check if the original asset has changed (last modified timestamp) which again increases loading times. Of course, this only applies if you have any filters assigned to them.

To avoid such problems, it is advised to turn off asset processing and run the processing via cronjob or simply from the command line. Sprinkle has a special method for that: $this->sprinkle->bake();.

For example, you could set up a CLI controller in which you would call this method.

NOTE: this only applies to pre-defined assets. Assets that you load manually (via js() or css() method) will still be processed (if they have filters). Also, even if you disable processing, assets will be combined if needed (if such group has not been cached, for example).

$config['sprinkle']['disable_processing'] = TRUE;

Please see Baking Assets to learn more about this feature.

Clone this wiki locally