Mecha CMS

Mecha CMS blog and documentation.

Class Asset

Updated: Sunday, 07 August 2016

Handle the site assets.

The Asset class will first check whether the given file path is exists in the current active shield folder. If so, then the URL path will be relative to the current shield folder. Otherwise, the URL path will be relative to ASSET, otherwise, the URL path will be relative to ROOT.

Get Private Asset Path

echo Asset::path('path/to/file.css')

Result:

.\www\path\to\file.css

Get Public Asset URL

echo Asset::url('path/to/file.css');

Result:

http://mecha-cms.com/2016/path/to/file.css

Generating JavaScript Tag

echo Asset::javascript('path/to/file.js');

Result:

<script src="http://mecha-cms.com/2016/path/to/file.js"></script>

Generating Style Sheet Tag

echo Asset::stylesheet('path/to/file.css');

Result:

<link href="http://mecha-cms.com/2016/path/to/file.css" rel="stylesheet">

Generating Image Tag

echo Asset::image('path/to/file.jpg', ' alt="test"');

Result:

<img src="http://mecha-cms.com/2016/path/to/file.jpg" alt="test">

Generating Multiple Asset Tags

echo Asset::javascript(array(
    'path/to/file-1.js',
    'path/to/file-2.js',
    'path/to/file-3.js'
));

echo Asset::stylesheet(array(
    'path/to/file-1.css',
    'path/to/file-2.css',
    'path/to/file-3.css'
));

echo Asset::image(array(
    'path/to/file-1.jpg',
    'path/to/file-2.jpg',
    'path/to/file-3.jpg'
));

Check for Loaded Assets

Added since version 1.1.1.

if(Asset::loaded('path/to/file.js')) {
    echo '<code>path/to/file.js</code> already loaded.';
}

Prevent Certain Assets Loading

Added since version 1.1.2.

// Prevent loading `path/to/file-1.js`
Asset::ignore('path/to/file-1.js');
echo Asset::javascript('path/to/file-1.js'); // Returns nothing!

// Prevent loading `path/to/file-1.css` and `path/to/file-2.css`
Asset::ignore(array(
    'path/to/file-1.css',
    'path/to/file-2.css'
));

Check for Ignored Assets

Added since version 1.1.2.

if(Asset::ignored('path/to/file-1.css')) {
    echo Asset::stylesheet('path/to/file-1.compat.css')
}

Combine Multiple Asset Files

Added since version 1.1.2.

Merge multiple asset files into a single file.

Method 1:

echo Asset::merge(array(
    'path/to/file-1.css',
    'path/to/file-2.css',
    'path/to/file-3.css'
), 'combined.css');

echo Asset::merge(array(
    'path/to/file-1.js',
    'path/to/file-2.js',
    'path/to/file-3.js'
), 'combined.js');

echo Asset::merge(array(
    'path/to/file-1.png',
    'path/to/file-2.png',
    'path/to/file-3.png'
), 'combined.png');

Method 2:

echo Asset::stylesheet(array(
    'path/to/file-1.css',
    'path/to/file-2.css',
    'path/to/file-3.css'
), "", 'combined.css');

echo Asset::javascript(array(
    'path/to/file-1.js',
    'path/to/file-2.js',
    'path/to/file-3.js'
), "", 'combined.js');

echo Asset::image(array(
    'path/to/file-1.png',
    'path/to/file-2.png',
    'path/to/file-3.png'
), "", 'combined.png');

Combine Multiple Asset Files Then Minify

Added since version 1.1.2.

Prepending a .min. keyword just before the file name extension will automatically minify the copied files:

echo Asset::javascript(array(
    'path/to/file-1.js',
    'path/to/file-2.js',
    'path/to/file-3.min.js' // ← this file will not be re-minified
), "", 'combined.min.js');

echo Asset::javascript('path/to/file.js', "", 'file.min.js');

Hooks

Filter

FilterDescription
asset:urlWill affect the asset URL.
asset:sourceWill affect the raw asset URL input.
asset:pathWill affect the generated asset file path.
asset:inputWill affect the asset content before minify.
asset:outputWill affect the asset content after minify.
asset:stylesheetWill affect the HTML markup of the CSS assets.
asset:javascriptWill affect the HTML markup of the JavaScript assets.
asset:imageWill affect the HTML markup of the image assets.

Weapon

None.

Donation and Email Subscription