Symfony2资产包问题

时间:2015-07-31 10:10:05

标签: php css assetic symfony-2.7

config.yml:

assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ FrontendBundle ]
    #java: /usr/bin/java
    ruby: /usr/bin/ruby
    sass: /usr/bin/sass
    read_from:      %kernel.root_dir%/../../web/
    write_to:       %kernel.root_dir%/../../web/
    filters:
        cssrewrite: ~
        scss:
            style: 'expanded'
            compass: true
            apply_to: "\.scss$"
        compass:
            bin: /usr/bin/compass
            images_dir: %kernel.root_dir%/../../src/FrontendBundle/Resources/assets/images
            generated_images_path: %kernel.root_dir%/../../src/FrontendBundle/Resources/public/images/sprites
            http_generated_images_path: /images/sprites

模板:

<?php foreach ($view['assetic']->stylesheets(
  array('@FrontendBundle/Resources/assets/scss/homepage.scss'),
  array('compass'),
  array('output' => 'css/homepage.css')
) as $url):
  $styles[] = '<link rel="stylesheet" href="'. $view->escape($url) .' " />' ?>
<?php endforeach; ?>

Prod环境工作得很好。生成css / homepage.css,结果html中有链接。

开发环境存在问题。如果我尝试动态生成css,我会得到&#34;无法生成指定路由的URL ...&#34;错误,&#34;捆绑&#34; config_dev.yml中设置的选项和roting_dev.yml中的资产路由。这只是我在这个问题上找到的解决方案,但都没有。

如果我尝试做类似prod,设置&#34; use_controller&#34;为false,在结果html中我得到了链接到&#34; homepage_homepage_1.css&#34;而不是&#34; homepage.css&#34;,而不是生成。

1 个答案:

答案 0 :(得分:0)

您是否为 Symfony 2.7 尝试了新的Asset component

应用/配置/ config.yml

framework:
    assets:
        version: 'v5'
        version_format: '%%s?version=%%s'
        base_path: ~
        base_urls: ['http://cdn.example.com', 'https://secure.example.com']
        packages:
           scss:
               base_path: bundles/mybundle/scss
           compass:
               base_path: bundles/mybundle/compass

然后你应该回忆一下这样的样式表:

{{ asset('myscss.css', 'scss') }}
{# /bundles/mybundle/scss/myscss.css #}

{{ asset('mycompass.css', 'compass') }}
{# /bundles/mybundle/compass/mycompass.css #}

我建议您在视图上使用Twig,以便过滤变量through an if statement(例如assetic)。