PHPackages                             odan/twig-assets - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. odan/twig-assets

ActiveLibrary[Templating &amp; Views](/categories/templating)

odan/twig-assets
================

Caching and compression for Twig assets (JavaScript and CSS).

4.1.0(11mo ago)2323.7k↓64.6%84MITPHPPHP 8.2.\* || 8.3.\* || 8.4.\*CI passing

Since Nov 26Pushed 7mo ago2 watchersCompare

[ Source](https://github.com/odan/twig-assets)[ Packagist](https://packagist.org/packages/odan/twig-assets)[ Docs](https://github.com/odan/twig-assets)[ RSS](/packages/odan-twig-assets/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (9)Versions (26)Used By (4)

Twig Assets Extension
=====================

[](#twig-assets-extension)

Caching and compression for Twig assets (JavaScript and CSS), inspired by [Symfony Web Assets](https://symfony.com/doc/3.0/best_practices/web-assets.html).

[![Latest Version on Packagist](https://camo.githubusercontent.com/70ace17e51e24b8420ba94619ef972491a3519c6beccc2faad3ca07af71da157/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6f64616e2f747769672d6173736574732e737667)](https://github.com/odan/twig-assets/releases)[![Software License](https://camo.githubusercontent.com/074b89bca64d3edc93a1db6c7e3b1636b874540ba91d66367c0e5e354c56d0ea/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](LICENSE.md)[![Build Status](https://github.com/odan/twig-assets/workflows/build/badge.svg)](https://github.com/odan/twig-assets/actions)[![Total Downloads](https://camo.githubusercontent.com/990308de76dbf2ebd69a36383a184e369de0555588b12a379d39279cf97f7241/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f64616e2f747769672d6173736574732e737667)](https://packagist.org/packages/odan/twig-assets/stats)

Installation
------------

[](#installation)

```
composer require odan/twig-assets

```

Requirements
------------

[](#requirements)

- PHP 8.2, 8.3, 8.4
- Twig 3

Configuration
-------------

[](#configuration)

```
$options = [
    // Public assets cache directory
    'path' => '/var/www/example.com/htdocs/public/assets/cache',

    // Public cache directory permissions (octal)
    // You need to prefix mode with a zero (0)
    // Use -1 to disable chmod
    'path_chmod' => 0750,

    // The public url base path
    'url_base_path' => 'assets/cache/',

    // Internal cache settings
    //
    // The main cache directory
    // Use '' (empty string) to disable the internal cache
    'cache_path' => '/var/www/example.com/htdocs/temp',

    // Used as the subdirectory of the cache_path directory,
    // where cache items will be stored
    'cache_name' => 'assets-cache',

    // The lifetime (in seconds) for cache items
    // With a value 0 causing items to be stored indefinitely
    'cache_lifetime' => 0,

    // Enable JavaScript and CSS compression
    // 1 = on, 0 = off
    'minify' => 1
];
```

Integration
-----------

[](#integration)

### Register the Twig Extension

[](#register-the-twig-extension)

```
$loader = new \Twig\Loader\FilesystemLoader('/path/to/templates');
$twig = new \Twig\Environment($loader, array(
    'cache' => '/path/to/compilation_cache',
));

$twig->addExtension(new \Odan\Twig\TwigAssetsExtension($twig, $options));
```

### Slim 4 Framework

[](#slim-4-framework)

Requirements

- [Slim Framework Twig View](https://github.com/slimphp/Twig-View/tree/3.x)

Run:

```
composer require slim/twig-view

```

Add these settings:

```
// Twig settings
$settings['twig'] = [
    'path' => __DIR__ . '/../templates',
    // Should be set to true in production
    'cache_enabled' => false,
    'cache_path' => __DIR__ . '/../tmp/twig-cache',
];

// Twig assets cache
$settings['assets'] = [
    // Public assets cache directory
    'path' => __DIR__ . '/../public/cache',
    // Public url base path
    'url_base_path' => 'cache/',
    // Internal cache directory for the assets
    'cache_path' => __DIR__ . '/tmp/twig-assets',
    'cache_name' => 'assets-cache',
    //  Should be set to 1 (enabled) in production
    'minify' => 1,
];
```

Add a DI container definition.

*This examples uses [PHP-DI](https://github.com/slimphp/Twig-View/tree/3.x#usage)*

```
