Helper usage

Mark Story edited this page Nov 20, 2015 · 16 revisions

The AssetCompressHelper allows you to easily link to and define build files in your views. First start off by linking the helper in your Controller:

<?php
public $helpers = array(
	'AssetCompress.AssetCompress'
);

After adding the helper to your $helpers array, you can use the helper in two ways. First, you can link to build files defined in your Configuration file. Second, you can use the helper to define dynamic build files, that can then be linked into your HTML.

Linking to assets

Once you've defined build files, either through the Configuration file, or as dynamic build files, you can output the HTML tags that link the assets to your page. $this->AssetCompress->css() and $this->AssetCompress->script() let you create those tags.

<?php
echo $this->AssetCompress->css('site.css', $options);
echo $this->AssetCompress->script('libs.js', $options);

Both of these methods work in a similar way. If asset caching is disabled, a compressed asset is generated on the fly using the built-in controller. If asset caching is enabled, and the helper is able to locate the generated build file, it will link to that file instead. This process is transparent, and depends on the Configuration values set. $options allows you to set any attributes on the generated tag, and includes the special key raw.

If you need to generate Asset tags with URL's with the host name you can use the full option:

<?php
echo $this->AssetCompress->css('site.css', array('full' => true));
echo $this->AssetCompress->script('libs.js', array('full' => true));

Including all the components for a build file

Sometimes when you are having trouble debugging an issue, its helpful to include all the component files for a build file. You can use the raw option to force the helper to generate tags pointing to all the parts of build file instead of the build file:

<?php
echo $this->AssetCompress->css('site.css', array('raw' => true));

If site.css had been defined as using the following files:

  • reset.css
  • grid.css
  • styles.css

The above call, would generate 3 <link /> tags.

Note including 'raw' assets will skip all processing steps, so files that need a pre-processing step applied, may not work correctly.