odan/plates-asset-cache

此软件包已被废弃,不再维护。未建议替代软件包。

Plates 模板资源(JavaScript 和 CSS)的缓存和压缩。


README

Plates 模板资源(JavaScript 和 CSS)的缓存和压缩。

Latest Version on Packagist Software License Build Status Code Coverage Quality Score Total Downloads

安装

composer require odan/plates-asset-cache

要求

  • PHP 7.3+ 或 8.0+

配置

use League\Plates\Engine;
use Odan\PlatesAsset\AssetEngine;
use Odan\PlatesAsset\PlatesAssetExtension;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$engine = new Engine('/path/with/html/templates');

$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
];

// Register asset extension
$engine->loadExtension(new PlatesAssetExtension(new AssetEngine($engine, $options)));

用法

页面模板

模板文件: index.php

<?php /** @var League\Plates\Template\Template $this */ ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <base href="<?= $baseUrl; ?>" />
        <title>Demo</title>
        <?= $this->assets(['default.css', 'print.css'], ['inline' => true]); ?>
    </head>
    <body>
    <!-- content -->

    <!-- JavaScript assets -->
    <?= $this->assets(['mylib.js', 'page.js']); ?>
    </body>
</html>

渲染模板

$engine = new League\Plates\Engine('/path/to/templates');

echo $engine->render('index', ['baseUrl' => '']);

结果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <base href="" />
    <title>Demo</title>
    <style></style>
<style>@media print{.noprint{display:none}.navbar{visibility:hidden;display:none}.container{width:99%}.table{table-layout:fixed;width:99%;max-width:99%}}</style></head>
<body>
<!-- content -->

<!-- JavaScript assets -->
<script src="assets/file.3dd5380c0b893eea8a14e30ce5bfa4cb9aab011b.js"></script></body>
</html>

参数

  1. 参数: $assets
名称 类型 默认值 必需 描述
files 数组 [] 所有要发送到浏览器的资源(文件)。也支持Plates 文件夹myalias::myfile.js)。
  1. 参数: $options
名称 类型 默认值 必需 描述
inline 布尔值 false 定义浏览器是直接下载资源还是通过 URL 下载。
minify 布尔值 true 指定是否启用或禁用 JS/CSS 压缩。
name 字符串 file 定义 URL 中的输出文件名。

Slim 4 集成

在此示例中,我们使用 PHP-DI 软件包。

添加容器定义

<?php

use League\Plates\Engine;
use Odan\PlatesAsset\PlatesAssetExtension;
use Psr\Container\ContainerInterface;
use Slim\App;

// ...

return [
    // ...

    Engine::class => function (ContainerInterface $container) {
        $settings = $container->get('settings');
        $viewPath = $settings['plates']['path'];

        $engine = new Engine($viewPath);

        // The public url base path
        $baseUrl = $container->get(App::class)->getBasePath();
        $engine->addData(['baseUrl' => $baseUrl]);

        $options['url_base_path'] = $basePath;

        $engine->loadExtension(new PlatesAssetExtension(new AssetEngine($engine, $options)));

        return $engine;
    },
];

渲染模板并将内容写入响应流

$response->withHeader('Content-Type', 'text/html; charset=utf-8');

$response->getBody()->write($this->engine->render($name, $viewData));

return $response;

许可证

MIT 许可证(MIT)。请参阅许可证文件以获取更多信息。