/assets

Elegant asset management for PHP with versioning, caching and providers for various storage backends

Primary LanguagePHPOtherNOASSERTION

Nette Assets

Downloads this Month Tests Latest Stable Version License

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)