[英]Symfony2 assetic bundle Issue
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; ?>
產品環境工作正常。 css / homepage.css生成,並且在結果html中有指向它的鏈接。
開發環境存在問題。 如果我嘗試動態生成CSS,則會收到“無法為命名路由生成URL ...”錯誤,並在config_dev.yml中設置了“ bundles”選項,在roting_dev.yml中設置了資產路由。 這只是我針對此問題找到的解決方案,沒有一個起作用。
如果我嘗試通過將“ use_controller”設置為false來進行類似prod的操作,則結果html中將獲得指向“ homepage_homepage_1.css”的鏈接,而不是未生成的“ homepage.css”。
您是否嘗試過Symfony 2.7的新資產組件 ?
app / config / 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,以便可以通過if語句 (例如assetic
)過濾變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.