使用 Silex/SilexExtensions 和 Assetic 在 Twig 中动态 CSS/Javascript

发布于 2024-12-28 07:35:28 字数 2656 浏览 0 评论 0原文

我想要做什么:从数据库获取 CSS 属性并将其转储到 less 文件中。然后在其上应用 less/yui 压缩过滤器并将输出转储到我的树枝模板中。


让我直接进入正题:

我有一个使用 Silex 和 Twig 作为模板引擎的 PHP Web 应用程序。为了处理和缩小 css/js 文件,我尝试使用 Assetic 和 Silex-Twig/Assetic-Extensions。

我已经注册了 silex 扩展并设置了我想要使用的过滤器。现在我不知道如何将文件转储到我的树枝模板中。谷歌搜索让我一无所知。由于 lessfile 中的属性可以根据请求进行更改,因此我认为无法静态传递文件。

这是我对 silex 扩展的实现:

$oApp = new Silex\Application();

//$oApp['autoloader']->registerNamespace('Assetic', DIR_VENDOR.'/assetic/src');
//$oApp['autoloader']->registerNamespace('SilexExtension', DIR_VENDOR.'/silex-extension/src');
//$oApp['autoloader']->registerNamespace('Twig', DIR_VENDOR.'/twig/lib');

$oApp->register(
    new Silex\Provider\TwigServiceProvider(), array(
        'twig.path' => DIR_ROOT.'/src/templates',
        'twig.class_path' => DIR_VENDOR.'/twig/lib',
    ),
    new SilexExtension\AsseticExtension(), array(
        'assetic.class_path' => DIR_VENDOR.'/assetic/src',
        'assetic.path_to_web' => DIR_ASSETS,
        'assetic.options' => array(
            'debug' => false,
            'formulae_cache_dir' => DIR_TMP.'/Assetic/cache',
            'twig_support' => true
        ),
        'assetic.filters' => $oApp->protect(function($fm) {
            $fm->set('yui_css', new Assetic\Filter\Yui\CssCompressorFilter(
                DIR_YUI.'/yuicompressor-2.4.7.jar'
            ));
            $fm->set('yui_js', new Assetic\Filter\Yui\JsCompressorFilter(
                DIR_YUI.'/yuicompressor-2.4.7.jar'
            ));
            $fm->set('googlecc_js', new Assetic\Filter\GoogleClosure\CompilerJarFilter(
                DIR_GOOGLE_CC.'/compiler.jar'
            ));
        }),
        'assetic.assets' => $oApp->protect(function($am, $fm) {
            $am>-set('styles', new Assetic\Asset\AssetCache(
                new Assetic\Asset\GlobAsset(
                    __DIR__ . '/resources/css/*.css',
                    array($fm->get('yui_css'))
                ),
                new Assetic\Cache\FilesystemCache(DIR_TMP.'/Assetic/cache')
            ));
            $am->get('styles')->setTargetPath(DIR_ASSETS.'/css/styles.css');
        })
    )
);

由于 CSS 文件是通过 less 过滤器处理的(变量值应来自数据库),我需要保存/缓存输出文件。我认为我需要的是一个 LazyAssetManager 与一个将 output.css 写入缓存目录的 AssetWriter 结合使用?但我真的很难从我的树枝模板中获得正确的包含语法。以下实施似乎不起作用:

{% stylesheets 'path/to/my/css' 'another/path/to/my/css' filter='yui_css' output='path/to/output/directory/styles.css' %}
<link href="{{ asset_url }}" rel="stylesheet" media="screen" />
{% endstylesheets %}

我感谢每一篇关于我的担忧的帖子。

What I want to do: Get CSS properties from a database and dump it into a less file. Then apply a less/yui compress-filter on it and dump the output in my twig template.


Let me just come to the point right away:

I have a PHP web application using Silex and Twig as template engine. In order to process and minify the css/js files I'm trying to use Assetic and the Silex-Twig/Assetic-Extensions.

I have registered the silex extensions and set the filters I want to use. Now I have no clue whatsoever on how to dump the files inside my twig template. Google Search keeps me in the dark. Since the properties in the lessfile can change per request I think there is no way of a static delivery of the files.

This is my implementation of the silex extensions:

$oApp = new Silex\Application();

//$oApp['autoloader']->registerNamespace('Assetic', DIR_VENDOR.'/assetic/src');
//$oApp['autoloader']->registerNamespace('SilexExtension', DIR_VENDOR.'/silex-extension/src');
//$oApp['autoloader']->registerNamespace('Twig', DIR_VENDOR.'/twig/lib');

$oApp->register(
    new Silex\Provider\TwigServiceProvider(), array(
        'twig.path' => DIR_ROOT.'/src/templates',
        'twig.class_path' => DIR_VENDOR.'/twig/lib',
    ),
    new SilexExtension\AsseticExtension(), array(
        'assetic.class_path' => DIR_VENDOR.'/assetic/src',
        'assetic.path_to_web' => DIR_ASSETS,
        'assetic.options' => array(
            'debug' => false,
            'formulae_cache_dir' => DIR_TMP.'/Assetic/cache',
            'twig_support' => true
        ),
        'assetic.filters' => $oApp->protect(function($fm) {
            $fm->set('yui_css', new Assetic\Filter\Yui\CssCompressorFilter(
                DIR_YUI.'/yuicompressor-2.4.7.jar'
            ));
            $fm->set('yui_js', new Assetic\Filter\Yui\JsCompressorFilter(
                DIR_YUI.'/yuicompressor-2.4.7.jar'
            ));
            $fm->set('googlecc_js', new Assetic\Filter\GoogleClosure\CompilerJarFilter(
                DIR_GOOGLE_CC.'/compiler.jar'
            ));
        }),
        'assetic.assets' => $oApp->protect(function($am, $fm) {
            $am>-set('styles', new Assetic\Asset\AssetCache(
                new Assetic\Asset\GlobAsset(
                    __DIR__ . '/resources/css/*.css',
                    array($fm->get('yui_css'))
                ),
                new Assetic\Cache\FilesystemCache(DIR_TMP.'/Assetic/cache')
            ));
            $am->get('styles')->setTargetPath(DIR_ASSETS.'/css/styles.css');
        })
    )
);

Since the CSS files are being processed through a less filter (variable values should come from a database) I need to save/cache the output file. I think what I need is a LazyAssetManager in conjunction with a AssetWriter that writes the output.css to a cache directory? But I am really struggling hard to get the right include syntax from within my twig templates. The following implementation does not seem to work:

{% stylesheets 'path/to/my/css' 'another/path/to/my/css' filter='yui_css' output='path/to/output/directory/styles.css' %}
<link href="{{ asset_url }}" rel="stylesheet" media="screen" />
{% endstylesheets %}

I'm thankful for every posting regarding my concern.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2025-01-04 07:35:28

由于(您提到)less 文件中的属性可以根据请求进行更改,因此我认为您误用了 assetic(甚至可能更少)。听起来您正在尝试使用 assetic 作为较少的预处理器,但事实并非如此+没有理由缓存结果(这取决于它们实际上每个请求不同的频率)。

您没有详细了解您想要实现的目标,但我认为它类似于主题引擎,用户可以在其中更改其配色方案(和其他外观变量)。如果是这种情况,我认为您应该将大部分 CSS/less 文件放在一个模板中,对于所有请求都是通用的,然后在您的 silex 应用程序中设置一条路由,以使用数据库中的变量传递特定于主题的 css。

由于每个请求它们可能不同,我认为没有理由为每个请求在服务器上添加更少的预处理开销,因此您应该直接输出 CSS。如果您对此感到满意,最多可以让客户端处理 .less 文件。

Since (you mention) the properties in the less file can change per request, I think you're missusing assetic (and maybe even less). It sounds like you're trying to use assetic as a less preprocessor, which it's not + there's little reason to cache the result (this depends on how often they will actually differ per request).

You don't get into much detail about what you want to achieve, but I tuess it's something like a theming engine, where users can change their color scheme (and other appearance variables). If this is the case, I think you should put the bulk of your CSS/less files in one template, common for all requests, and then have a route in your silex app to deliver the theme-specific css with variables from the DB.

Since they can be different per request, I don't think there's a reason to add less preprocessing overhead on the server for each request, so you should output straight CSS. At most, you could let the client process the .less files, if you're comfortable with that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文