|
| 1 | +--- |
| 2 | +title: Assets |
| 3 | +category: frontend |
| 4 | +status: drafting |
| 5 | +enabled: true |
| 6 | +sort: 2 |
| 7 | +intro: |
| 8 | +--- |
| 9 | + |
| 10 | +## Introduction |
| 11 | + |
| 12 | +The Streams platform comes with a fluid and highly extensible asset management tool for organizing, registering, customizing, and including your frontend assets. |
| 13 | + |
| 14 | +### Asset Collections |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +```php |
| 19 | +use Streams\Core\Support\Facades\Assets; |
| 20 | + |
| 21 | +$collection = Assets::collection('footer'); |
| 22 | +``` |
| 23 | + |
| 24 | +```blade |
| 25 | +@verbatim{!! Assets::collection('footer') !!} // Outputs asset tags"@endverbatim |
| 26 | +``` |
| 27 | + |
| 28 | +## Adding Assets |
| 29 | + |
| 30 | +Use the `add()` method to add an asset to a **collection**. |
| 31 | + |
| 32 | +```php |
| 33 | +Assets::collection('footer')->add('resources/js/start.js'); |
| 34 | +``` |
| 35 | + |
| 36 | +```blade |
| 37 | +@verbatim{!! Assets::collection('footer')->add('resources/js/start.js') !!}@endverbatim |
| 38 | +``` |
| 39 | + |
| 40 | +### Asset Sources |
| 41 | + |
| 42 | +The first and only argument should be the source of the asset. The following asset sources are supported out of the box. |
| 43 | + |
| 44 | +#### Paths in the Filesystem |
| 45 | + |
| 46 | +Any non-executable asset path relative to the application's **8*public root** may be used. |
| 47 | + |
| 48 | +```blade |
| 49 | +@verbatim{!! Assets::collection('footer')->add('js/example.js') !!} // /public/js/example.js@endverbatim |
| 50 | +``` |
| 51 | + |
| 52 | +<!-- #### Configured Storage Disks |
| 53 | +
|
| 54 | +You may use any configured storage location as an asset source. |
| 55 | +
|
| 56 | +```blade |
| 57 | +@verbatim{!! Assets::collection('footer')->add('s3::js/example.js') !!}@endverbatim |
| 58 | +``` --> |
| 59 | + |
| 60 | +#### Remote URLs |
| 61 | + |
| 62 | +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. |
| 63 | + |
| 64 | +```blade |
| 65 | +@verbatim{!! Assets::collection('footer')->add('https://cdn.com/js/example.js') !!}@endverbatim |
| 66 | +``` |
| 67 | + |
| 68 | +#### Hinted Assets |
| 69 | + |
| 70 | +Hinted assets are prefixed with a `namespace::` that is replaced with a [registered path](#registering-paths). |
| 71 | + |
| 72 | +```blade |
| 73 | +// /public/vendor/anomaly/streams/ui/js/example.js@endverbatim |
| 74 | +@verbatim{!! Assets::collection('footer')->add('ui::js/example.js') !!} |
| 75 | +
|
| 76 | +// https://cdn.domain.com/js/example.js@endverbatim |
| 77 | +@verbatim{!! Assets::collection('footer')->add('cdn::js/example.js') !!} |
| 78 | +``` |
| 79 | + |
| 80 | + |
| 81 | +### Named Assets |
| 82 | +@todo finish "Named Assets" |
| 83 | + |
| 84 | +## Outputting Assets |
| 85 | + |
| 86 | +Use output methods to include assets from a **collection**. |
| 87 | + |
| 88 | +### Generating URLs |
| 89 | + |
| 90 | +Use the `url()` method to return a single asset URL. |
| 91 | + |
| 92 | +```blade |
| 93 | +@verbatim{!! Assets::url('ui::js/example.js') !!}@endverbatim |
| 94 | +``` |
| 95 | + |
| 96 | +You can also use the `urls()` method on a **collection** to return all URLs. |
| 97 | + |
| 98 | +```blade |
| 99 | +@verbatim{!! Assets::collection('urls')->urls() !!}@endverbatim |
| 100 | +``` |
| 101 | + |
| 102 | +### Including Assets |
| 103 | + |
| 104 | +Use the `tag()` method to return a single asset URL. An **attributes** array can be passed as a second parameter. |
| 105 | + |
| 106 | +```blade |
| 107 | +@verbatim{!! Assets::tag('ui::js/example.js', [ |
| 108 | + 'async' => true |
| 109 | +]) !!}@endverbatim |
| 110 | +``` |
| 111 | + |
| 112 | +You can also use the `tags()` method on a **collection** to return all tags. |
| 113 | + |
| 114 | +```blade |
| 115 | +@verbatim{!! Assets::collection('footer')->tags() !!}@endverbatim |
| 116 | +``` |
| 117 | + |
| 118 | +## Registering Paths |
| 119 | + |
| 120 | +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. |
| 121 | + |
| 122 | +```php |
| 123 | +use Streams\Core\Support\Facades\Assets; |
| 124 | + |
| 125 | +Assets::addPath('ui', 'vendor/anomaly/streams/ui'); |
| 126 | +Assets::addPath('cdn', 'https://cdn.domain.com'); |
| 127 | +``` |
| 128 | + |
| 129 | +You can now use the above path hints to resolve assets. |
| 130 | + |
| 131 | +```blade |
| 132 | +@verbatim{!! Assets::collection('footer')->add('ui::js/example.js') !!} // /public/vendor/anomaly/streams/ui/js/example.js@endverbatim |
| 133 | +@verbatim{!! Assets::collection('footer')->add('cdn::js/example.js') !!} // https://cdn.domain.com/js/example.js@endverbatim |
| 134 | +
|
| 135 | +@verbatim{!! Assets::url('ui::js/example.js') !!}@endverbatim |
| 136 | +@verbatim{!! Assets::url('cdn::js/example.js') !!}@endverbatim |
| 137 | +``` |
| 138 | + |
| 139 | +## Named Assets |
| 140 | + |
| 141 | +Use the `register()` method to **name** one or more **assets**. The assets parameter can be any valid source or array of sources. |
| 142 | + |
| 143 | +```php |
| 144 | +use Streams\Core\Support\Facades\Assets; |
| 145 | + |
| 146 | +Assets::register('ui/tables', [ |
| 147 | + 'ui::js/tables.js', |
| 148 | + 'ui::css/tables.css', |
| 149 | +]); |
| 150 | +``` |
| 151 | + |
| 152 | +You can now use the collection's `load()` method to load the assets by **name**. |
| 153 | + |
| 154 | +```blade |
| 155 | +@verbatim{!! Assets::collection('footer')->load('ui/tables') !!}@endverbatim |
| 156 | +``` |
| 157 | + |
| 158 | +```php |
| 159 | +Assets::collection('footer')->load('ui/tables'); |
| 160 | +``` |
| 161 | + |
| 162 | +You can also render the output of the named single assets. |
| 163 | + |
| 164 | +```php |
| 165 | +Assets::tags('ui/tables'); |
| 166 | +``` |
0 commit comments