odan/twig-assets
Composer 安装命令:
composer require odan/twig-assets
包简介
Caching and compression for Twig assets (JavaScript and CSS).
README 文档
README
Caching and compression for Twig assets (JavaScript and CSS), inspired by Symfony Web Assets.
Installation
composer require odan/twig-assets
Requirements
- PHP 8.2, 8.3, 8.4
- Twig 3
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
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
Requirements
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
<?php 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; }, ];
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.
use Slim\Views\Twig; use Slim\Views\TwigMiddleware; // ... $app->add(TwigMiddleware::createFromContainer($app, Twig::class));
Add a route, e.g. in confg/routes.php:
$app->get('/', \App\Action\Home\HomeAction::class)->setName('root');
Create a action class, e.g. src/Action/HomeAction.php:
<?php namespace App\Action\Home; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Slim\Views\Twig; /** * Action. */ final class HomeAction { /** * @var Twig */ private $twig; /** * The constructor. * * @param Twig $twig The twig engine */ public function __construct(Twig $twig) { $this->twig = $twig; } public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface { return $this->twig->render($response, 'home/home.twig'); } }
The (pseudo) content of templates/home/home.twig:
<html> <head> {{ assets({files: ['home/index.css']}) }} </head> <body> {{ assets({files: ['home/index.js']}) }} </body> </html>
Read more: Usage
Slim 3 Framework
Requirements
In your dependencies.php or wherever you add your Service Factories:
$container[\Slim\Views\Twig::class] = function (Container $container) { $settings = $container->get('settings'); $viewPath = $settings['twig']['path']; $twig = new \Slim\Views\Twig($viewPath, [ 'cache' => $settings['twig']['cache_enabled'] ? $settings['twig']['cache_path']: false ]); /** @var \Twig\Loader\FilesystemLoader $loader */ $loader = $twig->getLoader(); $loader->addPath($settings['public'], 'public'); // Instantiate and add Slim specific extension $basePath = rtrim(str_ireplace('index.php', '', $container->get('request')->getUri()->getBasePath()), '/'); $twig->addExtension(new \Slim\Views\TwigExtension($container->get('router'), $basePath)); // Add the Assets extension to Twig $twig->addExtension(new \Odan\Twig\TwigAssetsExtension($twig->getEnvironment(), $settings['assets'])); return $twig; };
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
{{ assets({files: ['Login/login.css']}) }}
Output cached and minified CSS content inline:
{{ assets({files: ['Login/login.css'], inline: true}) }}
Output multiple CSS assets into a single .css file:
{{ assets({files: [
'@public/css/default.css',
'@public/css/print.css',
'User/user-edit.css'
], name: 'layout.css'})
}}
Output cached and minified JavaScript content
{{ assets({files: ['Login/login.js']}) }}
Output multiple JavaScript assets into a single .js file:
{{ assets({files: [
'@public/js/my-js-lib.js',
'@public/js/notify.js',
'Layout/app.js'
], name: 'layout.js'})
}}
Output page specific assets
Content of file: layout.twig
<html> <head> {% block assets %}{% endblock %} </head> <body> {% block content %}{% endblock %} </body> </html>
Content of home.twig:
{% extends "Layout/layout.twig" %}
{% block assets %}
{{ assets({files: ['Home/home.js'], name: 'home.js'}) }}
{{ assets({files: ['Home/home.css'], name: 'home.css'}) }}
{% endblock %}
{% block content %}
<div id="content" class="container"></div>
{% endblock %}
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.
{{ assets({files: [
'@public/css/default.css',
'@public/css/print.css',
'User/user-edit.css'
], attributes: {
rel: 'preload',
as: 'style',
onload: 'this.onload=null;this.rel=\'stylesheet\''
}, name: 'layout.css'})
}}
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:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <!-- other stuff --> <base href="{{ base_path() }}/"/> <!-- other stuff -->
Clearing the cache
Clearing the internal cache
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();
Clearing the public cache
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();
Testing
composer test
Similar libraries
License
The MIT License (MIT). Please see License File for more information.
odan/twig-assets 适用场景与选型建议
odan/twig-assets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.73k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2017 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「assets」 「javascript」 「cache」 「css」 「minify」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 odan/twig-assets 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 odan/twig-assets 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 odan/twig-assets 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Asset Management for PHP
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
A pretty nice way to expose your translation messages to your JavaScript.
统计信息
- 总下载量: 23.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 4
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-26