nette / assets
🎨 Nette Assets: elegant asset management for PHP with versioning, caching and mappers for various storage backends.
Requires
- php: 8.1 - 8.4
- nette/utils: ^4.0
Requires (Dev)
- latte/latte: ^3.0.18
- nette/di: ^3.2
- nette/tester: ^2.5
- phpstan/phpstan-nette: ^1.0
- tracy/tracy: ^2.9
Suggests
- latte/latte: Allows using Assets in templates
Conflicts
- nette/http: <3.3.1
This package is auto-updated.
Last update: 2024-12-15 20:08:08 UTC
README
Introduction
Nette Assets is a powerful asset management library for PHP that helps you:
✅ organize and serve your static assets (images, CSS, JavaScript, audio, etc.)
✅ handle asset versioning automatically
✅ get image dimensions without hassle
✅ verify asset existence in development mode
✅ support multiple storage backends
The library provides a clean and intuitive API to manage static assets in your web applications with focus on developer experience and performance.
Installation and Requirements
The recommended way to install is via Composer:
composer require nette/assets
Nette Assets requires PHP 8.1 or higher.
Usage
First, configure your assets in your application's configuration file:
assets: mapping: default: assets # maps 'default:' prefix to /assets directory audio: media/audio # maps 'audio:' prefix to /media/audio directory
Then use assets in your Latte templates:
<script src={asset('app.js')} defer></script>
You can also use mapper-specific prefixes:
<audio src={asset('audio:podcast.mp3')} controls></audio>
Asset Versioning
The library automatically appends version query string to asset URLs based on file modification time:
{asset('app.js')}
generates for example:
/assets/app.js?v=1699944800
This ensures proper cache invalidation when assets change.
Image Dimensions
Get image dimensions easily in templates:
<img src={asset('logo.png')} width={assetWidth('logo.png')} height={assetHeight('logo.png')}>
Multiple Storage Backends
The library supports multiple mappers, which can be configured independently:
assets: mapping: product: App\UI\Accessory\ProductMapper(https://img.example.com, %rootDir%/www.img)