dstbtam8732 2015-07-31 10:10
浏览 63

Symfony2资产包问题

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

Template:

<?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 environment works just fine. css/homepage.css is generated, and there is link to it in result html.

Problems are with dev environment. If I try to generate css dynamically i get "Unable to generate a URL for the named route..." error, with "bundles" option set in config_dev.yml and assetic routes in roting_dev.yml. This is only solutions I found for this issue, none of them worked.

If I try to do prod-like, with setting "use_controller" to false, in result html I get link to "homepage_homepage_1.css" instead of "homepage.css", which is not generated.

  • 写回答

1条回答 默认 最新

  • doushi7314 2015-08-14 06:17
    关注

    Have you tried the new Asset component for Symfony 2.7?

    <kbd>app/config/config.yml</kbd>

    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
    

    Then you should recall the correct stylesheet with something like that:

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

    I suggest you to use Twig on your views so you could filter your variables through an if statement (like assetic).

    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序