1. Go to this page and download the library: Download odan/twig-assets library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
odan / twig-assets example snippets
$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
];
$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));
// 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,
];
use Odan\Twig\TwigAssetsExtension;
use Psr\Container\ContainerInterface;
use Slim\App;
use Slim\Factory\AppFactory;
use Slim\Views\Twig;
use Twig\Loader\FilesystemLoader;
return [
// ...
Twig::class => function (ContainerInterface $container) {
$settings = $container->get('settings');
$twigSettings = $settings['twig'];
$twig = Twig::create($twigSettings['path'], [
'cache' => $twigSettings['cache_enabled'] ? $twigSettings['cache_path'] : false,
]);
$loader = $twig->getLoader();
if ($loader instanceof FilesystemLoader) {
$loader->addPath($settings['public'], 'public');
}
$environment = $twig->getEnvironment();
// Add Twig extensions
$twig->addExtension(new TwigAssetsExtension($environment, (array)$settings['assets']));
return $twig;
},
];
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
// ...
$app->add(TwigMiddleware::createFromContainer($app, Twig::class));
use Odan\Twig\TwigAssetsCache;
$settings = $container->get('settings');
// Internal twig cache path e.g. tmp/twig-cache
$twigCachePath = $settings['twig']['cache_path'];
$internalCache = new TwigAssetsCache($twigCachePath);
$internalCache->clearCache();
use Odan\Twig\TwigAssetsCache;
$settings = $container->get('settings');
// Public assets cache directory e.g. 'public/cache' or 'public/assets'
$publicAssetsCachePath = $settings['assets']['path'];
$internalCache = new TwigAssetsCache($publicAssetsCachePath);
$internalCache->clearCache();
twig
{{ assets({files: ['Login/login.js']}) }}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.