Download the PHP package odan/twig-assets without Composer
On this page you can find all versions of the php package odan/twig-assets. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download odan/twig-assets
More information about odan/twig-assets
Files in odan/twig-assets
Package twig-assets
Short Description Caching and compression for Twig assets (JavaScript and CSS).
License MIT
Homepage https://github.com/odan/twig-assets
Informations about the package twig-assets
Twig Assets Extension
Caching and compression for Twig assets (JavaScript and CSS), inspired by Symfony Web Assets.
Installation
Requirements
- PHP 8.2, 8.3
- Twig 3
Configuration
Integration
Register the Twig Extension
Slim 4 Framework
Requirements
Run:
Add these settings:
Add a DI container definition.
This examples uses PHP-DI
Add the TwigMiddleware
. In this case we pass the full
class name Twig::class
as the second parameter, because the
container entry is defined with the same name.
Add a route, e.g. in confg/routes.php
:
Create a action class, e.g. src/Action/HomeAction.php
:
The (pseudo) content of templates/home/home.twig
:
Read more: Usage
Slim 3 Framework
Requirements
In your dependencies.php
or wherever you add your Service Factories:
Usage
Custom template functions
This Twig extension exposes a custom assets()
function to your Twig templates. You can use this function to generate complete URLs to any Slim application assets.
Parameters
Name | Type | Default | Required | Description |
---|---|---|---|---|
files | array | [] | yes | All assets to be delivered to the browser. Namespaced Twig Paths (@mypath/ ) are also supported. |
inline | bool | false | no | Defines whether the browser downloads the assets inline or via URL. |
minify | bool | true | no | Specifies whether JS/CSS compression is enabled or disabled. |
name | string | file | no | Defines the output file name within the URL. |
nonce | string | no | The CSP (content security policy) nonce (per request) |
Template
Output cached and minified CSS content
Output cached and minified CSS content inline:
Output multiple CSS assets into a single .css file:
Output cached and minified JavaScript content
Output multiple JavaScript assets into a single .js file:
Output page specific assets
Content of file: layout.twig
Content of home.twig
:
Add custom attributes to the html element
WARNING: you can override ANY attribute including i.e. href
. Be careful here as it can cause unwanted results.
Configure a base path
You should inform the browser where to find the web assets with a base href
in your layout template.
Slim Twig example:
Clearing the cache
Clearing the internal cache
Clearing the public cache
Testing
Similar libraries
License
The MIT License (MIT). Please see License File for more information.
All versions of twig-assets with dependencies
mrclay/jsmin-php Version ^2.4
symfony/cache Version ^4.2.12 || ^5 || ^6 || ^7
tubalmartin/cssmin Version ^4.1