rvanginneken/asset-bundle

在任何位置包含资产,这些资产将被缓存并在页面的 或 结尾注入。

3.2.0 2019-11-21 09:51 UTC

This package is auto-updated.

Last update: 2024-09-21 20:41:14 UTC


README

AssetBundle 使得在 twig 文件中的任何位置包含资产变得简单,并在页面 和 标签的结尾渲染它们。不需要任何块。样式表以内联方式打印,所有结果在生产中均被缓存,遵循 Symfony 的 cache.app 设置。

在调试模式下,实际资产将被提供。在没有调试模式下,文件将被复制到 public/asset_cache 并使用唯一的命名。目录将自动由 cache:clear 命令清除。使用唯一的文件名确保在清除应用程序缓存时浏览器缓存失效。

安装

使用 composer 需求包

    $ composer require rvanginneken/asset-bundle

config/bundles.php 中启用包(在 Symfony 4 之前使用 AppKernel.php

    return [
        // ..
        RVanGinneken\AssetBundle\RVanGinnekenAssetBundle::class => ['all' => true],
        // ..
    ];

忽略资产缓存目录(用于破坏浏览器缓存)。在您的 .gitignore 中添加以下内容(在 Symfony 4 之前使用 web

    # ..
    /public/asset_cache/
    # ..

就这样。

使用方法

注意:优先级是可选的,并且仅添加到这些示例中以显示它们的可用性。

包含样式表

    {% asset 'css_file', 'css/hello_world.css', 0 %}

包含内联样式

    {%- set inline_style_hello_world -%}
        <style>
            body {
                background-color: lightblue;
            }
        </style>
    {%- endset -%}
  
    {% asset 'css', inline_style_hello_world, 0 %}

包含 JavaScript 文件

    {% asset 'javascript_file', 'js/hello_world.js', 0 %}

包含内联 JavaScript

    {%- set inline_javascript_hello_world -%}
        <script type="text/javascript">
            console.log('Hello world!');
        </script>
    {%- endset -%}
    
    {% asset 'javascript', inline_javascript_hello_world, 0 %}

公共文件夹

包基于 Symfony 版本检测公共文件夹。如果您使用的是自定义的,可以通过以下方式覆盖它:

    rvanginneken_asset:
        public_folder: your_custom_folder