Wallogit.com
2017 © Pedro Peláez
Link CSS / JS files in Laravel Blade templates
This package makes it easier to link CSS / JS files in Laravel Blade templates., (*1)
composer require mediactive-digital/laravel-asset-aliases
Add the service provider to /config/app.php file., (*2)
'providers' => [
// ...
MediactiveDigital\LaravelAssetAliases\LaravelAssetAliasesServiceProvider::class,
],
Add the AssetManager facade to /config/app.php file., (*3)
'aliases' => [
// ...
'MDAsset' => MediactiveDigital\LaravelAssetAliases\LaravelAssetAliasesFacade::class,
],
In order to avoid cache issues, especially with nginx, this package add specific timestamp in the filename of js & css files. home.js will be called as home.{timestamp}.js. You will need to add the following rule to your .htaccess file., (*4)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)\.[0-9]+\.(css|js)$ /$1.$2 [L]
</IfModule>
You can link either an internal resource or an external URL., (*5)
simple file, (*6)
{!! MDAsset::addCss('stylesheet.css') !!}
or array of files, (*7)
{!! MDAsset::addCss(['stylesheet.css', 'stylesheet2.css']) !!}
simple file, (*8)
{!! MDAsset::addJs('script.js') !!}
or array of files, (*9)
{!! MDAsset::addJs(['script.js', 'script2.js']) !!}
Add aliases to the configutation file located at /config/laravel-asset-aliases/alias.php., (*10)
// Example
return [
'css' => [
'stylesheet' => 'stylesheet.css'
],
'js' => [
'script' => 'script.js'
]
];
Then link files by aliases, for example :, (*11)
{!! MDAsset::addCss('stylesheet') !!}
You can pass any number of HTML attributes along with your file definition.\ It can be done both in configuration file and / or Blade templates.\ Attributes defined in Blade templates override configuration ones., (*12)
If you do so, you must use an associative array with the mandatory keys "file" and "attributes".\ "file" being your file (string) and "attributes" being your HTML attribute(s) (array)., (*13)
// Examples
// Inside configuration file :
return [
'css' => [
'stylesheet' => [
'file' => 'stylesheet.css',
'attributes' => [
'media' => 'print',
'title' => 'title'
]
],
'other-stylesheet' => [
'file' => 'other-stylesheet.css',
'attributes' => [
'media' => 'screen'
]
]
],
'js' => [
'script' => [
'file' => 'script.js',
'attributes' => [
'integrity' => 'sha384-rAnDoMkeY',
'crossorigin' => 'anonymous'
]
],
'other-script' => [
'file' => 'other-script.js',
'attributes' => [
'async' => null
]
]
]
];
// Inside Blade template :
{!! MDAsset::addCss([
[
'file' => 'stylesheet',
'attributes' => [
'media' => 'print',
'title' => 'title'
]
],
[
'file' => 'other-stylesheet',
'attributes' => [
'media' => 'screen'
]
]
]) !!}
{!! MDAsset::addJs([
[
'file' => 'script.js',
'attributes' => [
'integrity' => 'sha384-rAnDoMkeY',
'crossorigin' => 'anonymous'
]
],
'other-script' => [
'file' => 'other-script.js',
'attributes' => [
'async' => null
]
]
]) !!}