slexx / assets-manager
资产管理
dev-master
2019-04-24 20:41 UTC
This package is auto-updated.
Last update: 2024-09-25 08:36:56 UTC
README
这是一个用于管理脚本和样式的包,它保存了依赖项的加载顺序,防止多次加载同一个脚本。
安装
composer require slexx/assets-manager
使用
$manager = Slexx\AssetsManager\Manager::getInstance(); $head = $manager->head; $head->style('app-css', 'https://exmaple.com/assets/css/app.css', ['nunito']); $head->style('nunito', 'https://fonts.googleapis.com/css?family=Nunito'); echo $head; $foot = $manager->foot; $foot->script('app-js', 'https://exmaple.com/assets/js/app.js'); echo $foot;
集成
Laravel
安装
在文件 config/app.php
的 providers
部分,需要添加
Slexx\AssetsManager\Laravel\AssetsServiceProvider::class,
在 aliases
部分
'Assets' => Slexx\AssetsManager\Laravel\Assets::class,
门面
门面的使用示例
$head = Assets::location('head'); $head->style('app-css', 'https://exmaple.com/assets/css/app.css', ['nunito']); $head->style('nunito', 'https://fonts.googleapis.com/css?family=Nunito'); echo $head; $foot = Assets::location('foot'); $foot->script('app-js', 'https://exmaple.com/assets/js/app.js'); echo $foot;
指令
存在指令 script
、endscript
、style
、endstyle
用于添加资源,以及 assets
用于指定资源输出的位置。
以下示例来自一个项目
@style('app-css', asset('css/app.css')) @style('toastr-css', 'https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css') @style('open-iconic', 'https://cdnjs.cloudflare.com/ajax/libs/open-iconic/1.1.1/font/css/open-iconic.min.css') @script('app-js', asset('js/app.js')) @script('toastr-js', 'https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js') @style('highlight-css', asset('ckeditor/plugins/codesnippet/lib/highlight/styles/default.css')) @script('highlight-js', asset('ckeditor/plugins/codesnippet/lib/highlight/highlight.pack.js')) @script('highlight-setup', ['highlight-js']) hljs.initHighlightingOnLoad(); @endscript @script('ajax-setup', ['app-js']) $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': '{{ csrf_token() }}', }, }); @endscript @script('notifications', ['app-js', 'toastr-js']) $(document).ready(function() { @foreach(['warning', 'success', 'error', 'info'] as $type) @if(session()->has($type)) toastr.{{ $type }}('', '{{ strip_tags(session()->get($type)) }}', { timeOut: 5000, positionClass: 'toast-bottom-right', }); @php session()->remove($type); @endphp @endif @endforeach }); @endscript @stack('assets') @assets