Assets

Introduction

The Streams platform comes with a fluid and highly extensible asset management tool for organizing, registering, customizing, and including your frontend assets.

Asset Collections

Assets are organized into collections which can be accessed and output later. You can access or create an asset collection using the Assets facade or alias.

use Streams\Core\Support\Facades\Assets;

$collection = Assets::collection('footer');
{!! Assets::collection('footer') !!} // Outputs asset tags"

Adding Assets

Use the add() method to add an asset to a collection.

Assets::collection('footer')->add('resources/js/start.js');
{!! Assets::collection('footer')->add('resources/js/start.js') !!}

Asset Sources

The first and only argument should be the source of the asset. The following asset sources are supported out of the box.

Paths in the Filesystem

Any non-executable asset path relative to the application's 8*public root may be used.

{!! Assets::collection('footer')->add('js/example.js') !!} // /public/js/example.js

Remote URLs

The URL of a remote asset may also be used. The allow_url_fopen PHP directive must be enabled to output inline or content methods for remote files.

{!! Assets::collection('footer')->add('https://cdn.com/js/example.js') !!}

Hinted Assets

Hinted assets are prefixed with a namespace:: that is replaced with a registered path.

// /public/vendor/anomaly/streams/ui/js/[email protected]
{!! Assets::collection('footer')->add('ui::js/example.js') !!} 

// https://cdn.domain.com/js/example.js
{!! Assets::collection('footer')->add('cdn::js/example.js') !!} 

Named Assets

@todo finish "Named Assets"

Outputting Assets

Use output methods to include assets from a collection.

Generating URLs

Use the url() method to return a single asset URL.

@verbatim{!! Assets::url('ui::js/example.js') !!}

You can also use the urls() method on a collection to return all URLs.

{!! Assets::collection('urls')->urls() !!}

Including Assets

Use the tag() method to return a single asset URL. An attributes array can be passed as a second parameter.

{!! Assets::tag('ui::js/example.js', [
    'async' => true
]) !!}

You can also use the tags() method on a collection to return all tags.

{!! Assets::collection('footer')->tags() !!}

Registering Paths

Use the addPath() method to register a namespace and path. The path parameter can be any path in the filesystem relative to the application's public root or a remote URL prefix.

use Streams\Core\Support\Facades\Assets;

Assets::addPath('ui', 'vendor/anomaly/streams/ui');
Assets::addPath('cdn', 'https://cdn.domain.com');

You can now use the above path hints to resolve assets.

{!! Assets::collection('footer')->add('ui::js/example.js') !!} // /public/vendor/anomaly/streams/ui/js/example.js
{!! Assets::collection('footer')->add('cdn::js/example.js') !!} // https://cdn.domain.com/js/example.js

{!! Assets::url('ui::js/example.js') !!}
{!! Assets::url('cdn::js/example.js') !!}

Named Assets

Use the register() method to name one or more assets. The assets parameter can be any valid source or array of sources.

use Streams\Core\Support\Facades\Assets;

Assets::register('ui/tables', [
    'ui::js/tables.js',
    'ui::css/tables.css',
]);

You can now use the collection's load() method to load the assets by name.

{!! Assets::collection('footer')->load('ui/tables') !!}
Assets::collection('footer')->load('ui/tables');

You can also render the output of the named single assets.

Assets::tags('ui/tables');