Asset Loader for your Nette projects
Require | Version |
---|---|
PHP | >= 5.6 |
nette/application | ^2.4 |
nette/di | ^2.4 |
Require-dev | Version |
---|---|
nette/tester | ^2.0 |
tracy/tracy | ^2.4 |
-
Install Asset Loader with composer
composer require grape-fluid/asset-loader
-
Register extension (in neon)
extensions: assets: Grapesc\GrapeFluid\AssetLoaderExtension
-
Set your public directory (mostly www) (in neon)
assets: config: wwwDir: %appDir%/../www
-
Set-up your first package (see all options / features below)
assets: yourPackage: js: # will create file in wwwDir/assets/yourPackage/js/main.js - %appDir%/modules/WebsiteModule/assets/js/main.js css: # will create file in wwwDir/assets/yourPackage/css/styles.css - %appDir%/modules/StyleModule/assets/css/style.css # you can also use external / remote assets - 'https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css' copy: # will copy all images from folder into wwwDir/assets/yourPackage/copy/ - %appDir%/modules/StyleModule/assets/images/*
-
Inject AssetsControl and create component in your BasePresenter
/** @var \Grapesc\GrapeFluid\AssetsControl\AssetsControl @inject */ public $assets; protected function createComponentAssets() { return $this->assets; }
-
Render your assets in template
{control assets:css} {control assets:js}
If you have too many files (for ex. scripts) that you want to include in your templates,
or just want to copy whole directory (for ex. with images),
you can use asterisk *
character.
assets:
yourPackage:
js:
- %appDir%/modules/WebsiteModule/assets/js/scripts/*
If you need, for any reason, copy file into public directory with custom name or custom directory,
just specify asset as array. You can use &
character that will be replaced with public assets directory folder.
assets:
yourPackage:
js:
# [ source, destination ]
- [ %appDir%/modules/WebsiteModule/assets/js/main.js, &/my/folder/test.js ]
You can also do the same with whole directories:
assets:
yourPackage:
js:
# [ source, destination ]
- [ %appDir%/modules/StyleModule/assets/images/*, &/images/ ]
Of course, quickstart doesn't do anything special but copy files and use them in your template files. But it's getting more interesting with these options.
You can limit assets by Nette-like links. By default all defined assets are enabled everywhere. You can combine multiple limits. By using limits Asset Loader will load only assets that matches your limit. You can easily create assets for backend / frontend, but also for sub-frontend modules, etc.
assets:
yourPackage:
limit:
- ":Module:.*"
assets:
yourPackage:
limit:
- ":Module:Presenter:.*"
- ":Module:AnotherPresenter:.*"
assets:
yourPackage:
# Will call $user->isAllowed('homepage')
- ['link' = ':Module:Presenter.*', 'auth' = 'homepage']
Create and define custom service that implements \Grapesc\GrapeFluid\Options\IAssetOptions
interface as written below.
We are automatically registering these as services sou you don't have to.
Inject through constructor in these services also works.
Now you are able to handle limits in any way you want.
For example enable / disable assets from your back-office.
assets:
config:
options:
# Must implement \Grapesc\GrapeFluid\Options\IAssetOptions
- \Your\Class\That\Handles\SomeOptions
- \Your\Class\That\Handles\AnotherOptions
yourPackage:
# Will call $class->getOption('inline.enabled') on every service
- ['link' = ':Module:Presenter.*', 'option' = 'inline.enabled']
Use !
in front of limit
assets:
yourPackage:
limit:
- "!:Module:Presenter:.*"
Sometimes, especially when using modules, it comes handy to sort / order your packages.
assets:
yourPackage:
order: [start|end]
assets:
yourPackage:
order:
[before|after]: anotherPackageName
or
assets:
yourPackage:
order:
type: [before|after]
position: anotherPackageName
assets:
yourPackage:
[disable|disabled]: true
By default Asset Loader operates in production mode, you can override this with following:
assets:
config:
debug: true
When debug mode is one, Asset Loader watch files for changes and deploy them automatically. It also works if you add new files into your packages.
assets:
config:
assetsDir: customDirectoryName
assets:
config:
dirPerm: 0511